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

In reply to Re: User input compared with generated list by RMGir
in thread User input compared with generated list by zuinc

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.