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.
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
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 ------ u...@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 ---------
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] b...@morrow.me.uk
BM> Quoth Uri Guttman <u...@stemsystems.com>:
>> >>>>> "JWK" == John W Krahn <som...@example.com> writes:
>>
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.