Click here to get back home

Dear gurus how can I extract an ARRAY from a scalar regex-wise

 HomeNewsGroups | Search | About
 comp.lang.perl.misc    Post an article   get this group's latest topics as an RSS feed add this group's latest topics to your My MSN content add this group's latest topics to your My Yahoo content
Subject Author Date
Dear gurus how can I extract an ARRAY from a scalar regex-wise advice please wireless 802.11 05-09-2008
Get Chitika Premium
Posted by advice please wireless 802.11 on May 9, 2008, 2:18 pm
Please log in for more thread options
This is not a homework assignment. I have written this already in

Let's say I want to extract all of the days of the week out of a
scalar like:

"We went to the beach on Monday but it turned out that Sunday would
have been better. The weather report Saturday said no rain until
Thursday but I asked Tuesday (a trick!) and she said it rained
Wednesday."

I want a regex/map/etc (no iterative clauses PLEASE!) that yields:

@days = qw( Monday Sunday Saturday Thursday Tuesday (Wednesday )

My best shot is:
my @days = keys %{ /((mon|tues|wednes|thurs|fri|satur|sun)day)/gi };

Which, oddly, doesn't seem to work. I say "oddly", because

/((mon|tues|wednes|thurs|fri|satur|sun)day)/gi

=

0 'Saturday'
1 'Satur'
2 'Thursday'
3 'Thurs'
4 'Tuesday'
5 'Tues'
6 'Wednesday'
7 'Wednes'

Yet %{ /((mon|tues|wednes|thurs|fri|satur|sun)day)/gi }

is an empty array!? *TILT*

Posted by Mirco Wahab on May 9, 2008, 2:30 pm
Please log in for more thread options
advice please wireless 802.11 on RH8 wrote:
> Let's say I want to extract all of the days of the week out of a
> scalar like:
> "We went to the beach on Monday but it turned out that Sunday would
> have been better. The weather report Saturday said no rain until
> Thursday but I asked Tuesday (a trick!) and she said it rained
> Wednesday."
> I want a regex/map/etc (no iterative clauses PLEASE!) that yields:
> @days = qw( Monday Sunday Saturday Thursday Tuesday (Wednesday )
> My best shot is:
> my @days = keys %{ /((mon|tues|wednes|thurs|fri|satur|sun)day)/gi };
>
> Which, oddly, doesn't seem to work. I say "oddly", because
>
> /((mon|tues|wednes|thurs|fri|satur|sun)day)/gi


Your solution was close. Just after some more hours
playing w/regular expressions - you'd have made it ;-)

Maybe you intended sth. like:

...
my @daynam = qw' mon tues wednes thurs fri satur sun ';
my $regexp = '((?:' . join('|', @daynam) . ')day)';
my @keys = $scalar =~ /$regexp/gi;
...

Regards

M.

Posted by John W. Krahn on May 9, 2008, 6:15 pm
Please log in for more thread options
advice please wireless 802.11 on RH8 wrote:
> This is not a homework assignment. I have written this already in
>
> Let's say I want to extract all of the days of the week out of a
> scalar like:
>
> "We went to the beach on Monday but it turned out that Sunday would
> have been better. The weather report Saturday said no rain until
> Thursday but I asked Tuesday (a trick!) and she said it rained
> Wednesday."
>
> I want a regex/map/etc (no iterative clauses PLEASE!) that yields:
>
> @days = qw( Monday Sunday Saturday Thursday Tuesday (Wednesday )
>
> My best shot is:
> my @days = keys %{ /((mon|tues|wednes|thurs|fri|satur|sun)day)/gi };
>
> Which, oddly, doesn't seem to work. I say "oddly", because
>
> /((mon|tues|wednes|thurs|fri|satur|sun)day)/gi
>
> =
>
> 0 'Saturday'
> 1 'Satur'
> 2 'Thursday'
> 3 'Thurs'
> 4 'Tuesday'
> 5 'Tues'
> 6 'Wednesday'
> 7 'Wednes'
>
> Yet %{ /((mon|tues|wednes|thurs|fri|satur|sun)day)/gi }
>
> is an empty array!? *TILT*

That is because %{ } dereferences a hash reference but the match
operator returns a list not a hash reference. To get it to work you
have to copy the list to an anonymous hash:

my @days = keys %{{ /((mon|tues|wednes|thurs|fri|satur|sun)day)/gi }};



John
--
Perl isn't a toolbox, but a small machine shop where you
can special-order certain sorts of tools at low cost and
in short order. -- Larry Wall

Posted by Uri Guttman on May 9, 2008, 6:22 pm
Please log in for more thread options

JWK> That is because %{ } dereferences a hash reference but the match
JWK> operator returns a list not a hash reference. To get it to work you
JWK> have to copy the list to an anonymous hash:

JWK> my @days = keys %{{ /((mon|tues|wednes|thurs|fri|satur|sun)day)/gi }};

that won't work either as it will use some of the grabbed things as keys
and others as values. and you have 2 grabs there which confuses things
even more. i would say the inner grab of the short names should be a
grouping with ?:. then you need a map to add a value to each key to make
it into input to the hashref. untested:


my @days = keys %{
        { map { $_ -> 1 } /((?:mon|tues|wednes|thurs|fri|satur|sun)day)/gi }
};

and another little thing is that | is slow in regexes. probably not a
problem for this case but it might be better grabbing all words that end
in day and counting them in a hash then extracting the good ones. i
leave that as an exercise.

uri

--
Uri Guttman ------ uri@stemsystems.com -------- http://www.sysarch.com --
----- Perl Code Review , Architecture, Development, Training, Support ------
--------- Free Perl Training --- http://perlhunter.com/college.html ---------
--------- Gourmet Hot Cocoa Mix ---- http://bestfriendscocoa.com ---------

Posted by Ben Morrow on May 9, 2008, 6:40 pm
Please log in for more thread options

>
> JWK> That is because %{ } dereferences a hash reference but the match
> JWK> operator returns a list not a hash reference. To get it to work you
> JWK> have to copy the list to an anonymous hash:
>
> JWK> my @days = keys %{{ /((mon|tues|wednes|thurs|fri|satur|sun)day)/gi }};
>
> that won't work either as it will use some of the grabbed things as keys
> and others as values. and you have 2 grabs there which confuses things
> even more.

It will 'work'. The two grabs mean you will be building a hash like

( monday => 'mon', friday => 'fri' )

which is certainly useful under some circumstances.

> i would say the inner grab of the short names should be a
> grouping with ?:. then you need a map to add a value to each key to make
> it into input to the hashref. untested:
>
>
> my @days = keys %{
>         { map { $_ -> 1 } /((?:mon|tues|wednes|thurs|fri|satur|sun)day)/gi }
> };

...or forget building a hash just to list its keys, and use

my @days = /((?:mon|tues|...)day)/gi;

?

Ben

--
For far more marvellous is the truth than any artists of the past imagined!
Why do the poets of the present not speak of it? What men are poets who can
speak of Jupiter if he were like a man, but if he is an immense spinning
sphere of methane and ammonia must be silent? [Feynmann] ben@morrow.me.uk

Similar ThreadsPosted
I need to extract an array from a scalar regex-wise ? May 9, 2008, 2:22 pm
How to extract all link from web page (loaded in variable $content) to scalar @links? September 11, 2006, 4:47 am
extract every other element from an array September 1, 2005, 2:25 pm
scalar to array? January 29, 2008, 1:50 pm
extract a column from 2 dimensional array September 13, 2004, 7:31 am
Using JOIN: Converting scalar to array August 23, 2005, 7:18 pm
assigning array to a scalar variable March 14, 2008, 4:39 am
How to pass an array and scalar as arguments to a subroutine/ June 28, 2006, 12:16 pm
substr forces scalar context with array argument November 29, 2005, 7:18 am
Mail::Box::Manager message body scalar or array? October 15, 2006, 8:33 pm

Our other projects:

Art Dolls, Fairies and Mermaids - Sunnyfaces.net

Roy's Linux, Programming and Search Engines messages

1-Script XML SitemapXML Sitemap