Tuesday, April 20, 2010

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

Programmer Question

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.



Find the answer here

No comments:

Post a Comment

LinkWithin

Related Posts with Thumbnails