Hi Noah,

On Tuesday 14 September 2010 17:23:09 Noah wrote:
> Hi there,
> 
> I am trying to figure out how to use grep in a particular situation.
> The line is:
> 
>          next unless grep ($fromdevice, @sitenamekeys);
> 
> 
> but I am not quite matching properly.
> 
> @sitenamekeys = /ABC DEF GHI/;
> 
> and $fromdevice could be something like "device1.abc" or "device2.def"
> basically I am trying to get the grep to identify if the current
> $fromdevice has anything that matching any of the elements in @sitenamekeys
> 
> So "device1.abc" or "device2.def" would be a match and therefore would
> continue without next but something like "device1.xyz" would not match
> and therefore next would be executed.
> 
> Any help is appreciated.
> 

grep accepts a block as the first argument, so it should read:

[code]
next unless grep {$_ eq $fromdevice} @sitenamekeys);
[/code]

In this case, you should prefer to one of:

1. Use List::MoreUtils::any instead of grep.

2. Use the perl-5.10 and above smart-match operator - ~~, which can see if an 
element exists in a list.

3. Use a hash for faster lookups.

Regards,

        Shlomi Fish

-- 
-----------------------------------------------------------------
Shlomi Fish       http://www.shlomifish.org/
First stop for Perl beginners - http://perl-begin.org/

God considered inflicting XSLT as the tenth plague of Egypt, but then
decided against it because he thought it would be too evil.

Please reply to list if it's a mailing list post - http://shlom.in/reply .

-- 
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/


Reply via email to