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