Greetings,

Pardon my ignorance. Used to write perl code for just this type of thing but haven't in over 10+ years! Have the occasion to get back into writing perl code and suspect I am making an obvious, basic mistake.

I have an array populated with zip codes. I want to read a text file and only print (output) the lines from the text file that have a zip codes from my array of zip codes.

# first populate array with zipcodes from zipcodes # text file. # --- No problem here my $file = "zipcodes.txt"; open (FH, "< $file") or die "Can't open $file for read: $!"; my @zipcodes; while (<FH>) { push (@zipcodes, $_); } close FH or die "Cannot close $file: $!"; # open the text file for processing looking only for # lines of text with a zipcode from the array @zipcodes # --- here is the problem as it outputs nothing while (my $line = <STDIN>) { foreach my $zipcode (@zipcodes) { if ($line =~ m/$zipcode/) { print $line; } } }

Have searched the site for doing just this, but examples all seemed to focus on elegance, efficiency and alternate ways to do it, which confused me even more as I am relearning perl. Would seem to be simple enough to do but scratching my head. Efficiency not an issue on this for me. Just need it to work, even if not terribly efficient.

Thank you for any insights. Jasper

In reply to regex matching array values with foreach by jasper8989

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.