How can I check if a value is in a list in Perl?

Posted by ablimit on Stack Overflow See other posts from Stack Overflow or by ablimit
Published on 2010-04-20T20:47:58Z Indexed on 2010/04/20 20:53 UTC
Read the original article Hit count: 139

Filed under:
|
|
|

I have a file in which every line is an integer which represents an id. What I want to do is just check whether some specific ids are in this list. But the code didn't work. It never tells me it exists even if 123 is a line in that file. I don't know why? Help appreciated.

open (FILE, "list.txt") or die ("unable to open !");

    my @data=<FILE>;

    my %lookup  =map {chop($_) => undef} @data;

    my $element= '123';
    if (exists $lookup{$element})
    {
        print "Exists";
    }

Thanks in advance.

© Stack Overflow or respective owner

Related posts about perl

Related posts about array