in reply to Re: check if a number is in a list
in thread check if a number is in a list

(for OP's benefit) the hash method:
my @array = (10,11,12,13,14,20,21,25); my $x = 1; my %array = map { $_ => undef } @array; if( exists $array{$x} ){ ... }

Replies are listed 'Best First'.
Re^3: check if a number is in a list
by Ido (Hermit) on Jun 26, 2005 at 05:21 UTC
    Is the map really needed? What about:
    my %array; @array{@array}=();
Re^3: check if a number is in a list
by 5mi11er (Deacon) on Jun 27, 2005 at 16:33 UTC
    No, the map isn't really needed, and might cause confusion, but if it does, I'd bet the @array{@array}=(); idiom isn't going to be understood either.

    OP, in both cases above, the idea is to have the numbers in your list to actually be in a hash rather than an array. The map and @array{@array} thing are simply shorthand ways to take your existing array and make it into a hash.

    Depending on your actual implementation, you might be better off if you created and added/removed the numbers meant to be within the list, and did so using hash operations instead. Then you wouldn't need the shorthand idioms above.

    -Scott