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. JasperIn reply to regex matching array values with foreach by jasper8989
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |