Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

looking for simple way to check scalar against array

by raflach (Pilgrim)
on May 19, 2005 at 14:49 UTC ( [id://458637]=perlquestion: print w/replies, xml ) Need Help??

raflach has asked for the wisdom of the Perl Monks concerning the following question:

What I want is something that will do "what I expect" of the following pseudo-code in perl

( $i in (1,2,3..9,-12) ) && do { print "$i is a 1, 2, 3, 4, 5, 6, 7, 8, 9, or -12\n"; }

I'm sure that I've seen this question covered before, but I couldn't find it--probably because I don't know the correct terminology to search for

Thanks

Replies are listed 'Best First'.
Re: looking for simple way to check scalar against array
by ikegami (Patriarch) on May 19, 2005 at 15:05 UTC
    And of course, the hash approach:
    my %in; undef @in{1, 2, 3..9, -12}; print "$i is a 1, 2, 3, 4, 5, 6, 7, 8, 9, or -12\n" if exists $in{$i};

    This method's advantage is that you can reuse the hash if you need to check if multiple variables are in the set, while still being extremely efficient.

Re: looking for simple way to check scalar against array
by Joost (Canon) on May 19, 2005 at 14:53 UTC
Re: looking for simple way to check scalar against array
by reasonablekeith (Deacon) on May 19, 2005 at 15:13 UTC
    And yet another way...

    use strict; use List::MoreUtils qw(any); my @list = (1,2,3..9,-12); my $i = -5; print "$i is a " . join(',', @list) . "\n" if any { $_ == $i } @list;
    ---
    my name's not Keith, and I'm not reasonable.
Re: looking for simple way to check scalar against array
by blazar (Canon) on May 19, 2005 at 14:55 UTC
    I like (and actually use)
    sub is { bless \shift, '_hidden'; } sub _hidden::in { my $s=${shift,}; $s eq $_ and return 1 for @_; 0; }
    use it thus:
    do_something if is($item)->in(@list);
Re: looking for simple way to check scalar against array
by Nevtlathiel (Friar) on May 19, 2005 at 14:54 UTC
    my @array = (1,2,3..9,-12); foreach my $variable (@array) { print "$variable is in the list\n"; }

    That something like what you want?

    Update: Oops, looks like I didn't really understand what you wanted, everyone else's suggestions are better :)

    ----------
    My cow-orkers were talking in punctuation the other day. What disturbed me most was that I understood it.
Re: looking for simple way to check scalar against array
by tcf03 (Deacon) on May 19, 2005 at 15:01 UTC
    Like this?
    #!/usr/bin/perl -w use strict; my $i = 4; my @nums = ( 1 .. 9 ); push (@nums, -12); for (@nums) { print if /$i/; }

    or
    #!/usr/bin/perl -w use strict; my $i = 4; for ( 1 .. 9, -12 ) { print if /$i/; }
    UPDATE
    What blazar ikegami and dug said.
    My example was to show you what you might not want to do, but may or may not work anyway.
    Ted
    --
    "That which we persist in doing becomes easier, not that the task itself has become easier, but that our ability to perform it has improved."
      --Ralph Waldo Emerson

      Why use a regexp? It's inefficient, and that particular regexp will fail if 1) $i contains special characters, and 2) if $i is part of another number (e.g. when $i=1 and list=(2..10), your regexp will match).

      print if /^\Q$i\E$/ would fix the problem.
      print if $_ eq $i is equivalent, but much more efficient.
      print if $_ == $i would be even more efficient given that we're dealing with numbers here.

      If the list contains anything with a 4 in it you will get a match, which I don't think is what the OP wants. If you are going to use the regex approach it should probably be anchored on both ends: /^$i$/.

      update: Not that you really needed a third person to remind you [grin].

      -- Douglas
      Gawd, believe me: I'm very keen on regexen myself, but I wouldn't use one where what you really want is an equality operator! And in that case it should be
      /^$i$/ if you qw/really wanted to do it like that/;

      Update: As duly pointed out by ikegami, to be really more precise, one should use /^\Q$i\E$/, which may seem nitpicking wrt this particular example, but which can be also absolutely necessary in other situations... (well, not that you would use this actual regex, but maybe a somewhat similar one.)

Log In?
Username:
Password:

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

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

    No recent polls found