Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
PerlMonks  

RE: Comparing Scalars and Arrays

by autark (Friar)
on Jul 19, 2000 at 21:31 UTC ( [id://23250]=note: print w/replies, xml ) Need Help??


in reply to Comparing Scalars and Arrays

If it smells like grep and looks like grep, then it must be grep :-)
if( grep { $scalar == $_ } @array ) { print "$scalar is member of @array\n"; } else { print "$scalar is not member of @array\n"; }
I see that you in your question has assigned an anonymous array to @array, if that really is the case, then you have to do some more work:
$scalar = 3; @array = ([1, 2], [3, 4, 5], [6, 7, 8 ,9]); if( grep { $scalar == $_ } map { @$_ } @array ) { print "Yes\n" } else { print "No\n" }
Autark.

Replies are listed 'Best First'.
RE: RE: Comparing Scalars and Arrays
by cwest (Friar) on Jul 19, 2000 at 21:36 UTC
    IMHO, your second example is great, but the condition seems like it could be more readable. Since you use grep, why not use it again, instead of map
    grep { grep { $scalar == $_ } @{$_} } @array
    Just MHO...

    Update:And it's faster, 1000000 iterations has Grep+Grep at 11 Walclocks and Grep+Map at 16.

    --
    Casey
    

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://23250]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others sharing their wisdom with the Monastery: (5)
As of 2024-04-24 07:05 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found