Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

Re: without looping, check if $var is found in @list

by leocharre (Priest)
on Dec 14, 2006 at 04:31 UTC ( [id://589745]=note: print w/replies, xml ) Need Help??


in reply to without looping, check if $var is found in @list

It should be pointed out your example is, i believe; horrendously wrong. In:

if ($var=~ m/@list/ ){ print 'ok'; }

What you are doing is attempting to match the @list inside of $var, and we know that $var does not hold all those things.

This would have worked:

#!/usr/bin/perl -w use strict; use warnings; my @list = qw(frog turtle tadpole); my $var = "turtle"; if ("@list" =~ /\b$var\b/) { print 'yup'; } # using quotes, we coerce the array to act as a string else { print 'nope'; }

We didn't have to place the \b for this example to appear as if it worked. The \b means word boundary. Otherwise, we would match on 'turtles' and 'turtley'.

the idea is : $lookinside =~ /$for/

Replies are listed 'Best First'.
Re^2: without looping, check if $var is found in @list
by duckyd (Hermit) on Dec 15, 2006 at 02:32 UTC
    Using string interpolation on the array is a bad idea - it won't neccessarily get you what you want. For example:
    #!/usr/bin/perl -w use strict; use warnings; my @list = ('jumping turtle', 'frog', 'tadpole'); my $var = "turtle"; if ("@list" =~ /\b$var\b/) { print "yup\n"; }
    happily reports a match even though just "turtle" doesn't appear in the array, it's a "jumping turtle". I would be reluctant to assume that such behavior is desirable...
Re^2: without looping, check if $var is found in @list
by ikegami (Patriarch) on Dec 15, 2006 at 04:49 UTC
    • It will fail (false positive or false negative) if $var contains regexp characters.

      my $var = 'tad?pole'; my @list = ($var); print("@list" =~ /\b$var\b/ ? 1 : 0, "\n");
    • It will fail (false negative) if $var starts or ends with \W (because \b won't match).

      my $var = 'frog!'; my @list = ($var); print("@list" =~ /\b$var\b/ ? 1 : 0, "\n");
    • It will fail (false positive or false negative) if $var contains spaces. (Credit goes to duckyd.)

      my $var = 'turtle'; my @list = ("jumping $var"); print("@list" =~ /\b$var\b/ ? 1 : 0, "\n");

    If you knew a specific character will not be present in @list, you could use that character as a seperator.

    my $sep = "\x01"; my @list = ('jumping turtle', 'frog!', 'tad?pole'); for my $var (@list) { if (join($sep, '', @list, '') =~ /\Q$sep$var$sep\E/) { print "Found $var\n" } else { print "Didn't find $var\n" } }

    You could even make it faster by using index.

    my $sep = "\x01"; my @list = ('jumping turtle', 'frog!', 'tad?pole'); for my $var (@list) { if (index(join($sep, '', @list, ''), "$sep$var$sep") >= 0) { print "Found $var\n" } else { print "Didn't find $var\n" } }

    But really, are either of those solutions anywhere near as readable as those already suggested? That's without even taking into account the limitation of disallowing $sep from being in @list.

    Update: A few small corrections: Removed extra parens, fixed third snippet.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others admiring the Monastery: (5)
As of 2024-04-12 22:09 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found