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

  • Thread starter advice please wireless 802.11 on RH8
  • Start date
A

advice please wireless 802.11 on RH8

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

Mirco Wahab

advice said:
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.
 
J

John W. Krahn

advice said:
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
 
U

Uri Guttman

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
 
B

Ben Morrow

Quoth Uri Guttman said:
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
 
U

Uri Guttman

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

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,902
Latest member
Elena68X5

Latest Threads

Top