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

#### Do you have a question? Post it now! No Registration Necessary.  Now with pictures!

•  Subject
• Author
• Posted on
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*

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

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.

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

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

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

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 ---------

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

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.

...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

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

>>
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.

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

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

BM> which is certainly useful under some circumstances.

maybe. but nutty and obscure as hell. nested grabs are not nice.

>> 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 }
>> };

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

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

that will get every instance of days and the OP only wanted unique
ones.

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 ---------