in reply to User input compared with generated list

I'm not sure your 2nd loop does what you intend.

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.

# 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"; }
That way, you only read in subnets.list once, and you can reuse it in your code.

If the file is expected to change, or you only need it once, then you'd have to do something like this:

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"; }
Hope this helps, and that I understood your problem correctly...
--
Mike

Replies are listed 'Best First'.
Re: Re: User input compared with generated list
by zuinc (Novice) on Apr 03, 2002 at 19:06 UTC
    Awesome! It actually did print PASS the first time but I wasnt able to see it amongst all those FAILS. But I only wanted a PASS or FAIL value printed once and I was guided in the right direction. Thanks everyone. ~Zuinc
      Glad to help!

      By the way, stephen is right; don't forget -w and use strict. They'll save you much grief in the long run.
      --
      Mike