when using grep() the expresion goes first and the search string second. So this
my @phonesearch = grep @name, @phonelisting; should be this
my @phonesearch = grep /@name/, @phonelisting;
edit:After reading the other replies I went back and looked up grep(). I was thinking it worked exactly like *NIX's grep but it seems I was mistaken. I've changed the above to relect this but I am unsure of the validity of putting an array in a REGEX. I would appreciate any input on this. I still stand by the below block as being my prefered way to preform this search.
Instead of using grep however; I would do your while loop something like this (untested code)
my @phonesearch;
my @name =<NL>;
chomp(@name);
my $name;
while(my $phonelisting = <PB>) {
chomp($phonelisting);
foreach $name (@name) {
if ($phonelisting =~ /$name/) {
push(@phonesearch,$phonelisting);
}
}
}
print OUT "@phonesearch\n";
notice I moved the declaration of @phonesearch outside the loop, this is so the value wouldn't expire when the loop ended. The reasons for my other changes are that using
while(1) is a bad idea. It sets you up for an infinite loop and could bite you in a future program if it becomes a habit. Also for this type of application it makes more sense to me to go through the lists one element at a time.
John
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: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.