|
Posted by nolo contendere on May 9, 2008, 2:40 pm
Please log in for more thread options On May 9, 2:22=A0pm, "advice please wireless 802.11 on RH8"
> This is not a homework assignment. I'd be interested in more elegante'
> solutions than mine is all..
>
> Let's say I want to extract all of the days of the week out of a
> scalar such as :
>
> $_ =3D
> '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/grep/etc (no iterative clauses PLEASE!) that
> yields:
>
> @days =3D qw( Monday Sunday Saturday Thursday Tuesday Wednesday )
>
> My best shot is:
> =A0my @days =3D keys %{ =A0/((mon|tues|wednes|thurs|fri|satur|sun)day)/gi =
};
>
> Which, oddly, doesn't seem to work. I say "oddly", because
>
> =A0 =A0/((mon|tues|wednes|thurs|fri|satur|sun)day)/gi
>
> =3D
>
> =A0 0 =A0'Saturday'
> =A0 1 =A0'Satur'
> =A0 2 =A0'Thursday'
> =A0 3 =A0'Thurs'
> =A0 4 =A0'Tuesday'
> =A0 5 =A0'Tues'
> =A0 6 =A0'Wednesday'
> =A0 7 =A0'Wednes'
>
> yet %{ =A0/((mon|tues|wednes|thurs|fri|satur|sun)day)/gi }
>
> is an empty array!? *TILT*
>
> I'm also not crazy about the hash solution because order may be
> significant.
you need non-capturing parens around the "roots" of your weekday and
weekend names.
use strict; use warnings;
use Data::Dumper;
$_ =3D
'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.' ;
my @days =3D /((?:mon|tues|wednes|thurs|fri|satur|sun)day)/gi;
print Dumper( \@days ), "\n";
$ ./extract_from_str.pl
$VAR1 =3D [
'Monday',
'Sunday',
'Saturday',
'Thursday',
'Tuesday',
'Wednesday'
];
|