in reply to User input compared with generated list
Your code compares $ip_subnet with EVERY entry in the subnets.list file, and gives PASS/FAIL for each.
I think what you want is a PASS if the entry appears anywhere in the list, and fail otherwise.
There's at least 2 ways to do this, one that's good if you're going to have to check a lot of $ip_subnet values against the same stable list, and one for checking just one value, or checking values against a changing list.
That way, you only read in subnets.list once, and you can reuse it in your code.# in your initialization... my %subnets; open(SUBNET, "<subnets.list") or die "Couldn't open subnets.list for reading, error $!\n"; while(<SUBNET>) { chomp; $subnets{$_}=1; } close(SUBNET); # for each read $ip_subnet: if(exists $subnets{$ip_subnet}) { print "PASS\n"; } else { print "FAIL\n"; }
If the file is expected to change, or you only need it once, then you'd have to do something like this:
Hope this helps, and that I understood your problem correctly...open(SUBNET, "<subnets.list") or die "Couldn't open subnets.list for reading, error $!\n"; my $exists=0; while(<SUBNET>) { chomp; if($_ eq $ip_subnet) { $exists++; last; } } close(SUBNET); if($exists) { print "PASS\n"; } else { print "FAIL\n"; }
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Re: User input compared with generated list
by zuinc (Novice) on Apr 03, 2002 at 19:06 UTC | |
by RMGir (Prior) on Apr 03, 2002 at 19:34 UTC |