If there are 21 fields in each record, and the 4th field is an email address, and you want @goodrecs to hold just those records having a specific email address in that field, then consider this question:

Is it ever possible that the email address you're searching for could occur in some other field but not in the 4th field? (I would assume that if "target@email.address" is in field 6 and not in field 4, then it's not a "goodrec".)

If the records containing the target string always have that string in field 4, then you don't have to split:

@goodrecs = grep /:target\@email\.address:/, @recs; # (note the delimiters are part of the match)
But if that condition does not hold for your data, then split is the best approach, as others have suggested. And in that regard, if you're actually looking for one specific string at a time (as opposed to, say, all email strings that have digits before the "@"), then it may be better not to use a regex:
@goodrecs = grep { @f=split/:/; $f[4] eq $target } @recs;
This avoids getting "bigjoe@hotmail.com" and "dumbjoe@hotmail.com" when you were looking for just "joe@hotmail.com", in case that matters to you.

(update: Maybe the latter snippet should say $f[3], if "field 4" is based on the first field being "field 1".)


In reply to Re: Re: grep fields by graff
in thread grep fields by sweetblood

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.