I liked your solution, and posted a version using split at Re: Find Not Working.

After some reflection, I think that something like this is probably better than either:

while (<DATA>) { if (my ($name_column_deleted) = m/((?:H0|HT)\d{8,}.*)/) { print "$name_column_deleted\n"; } }
The OP doesn't show what exactly can go in the "NAME" field but I suspect that it could contain spaces. "John Smith, Jr." or whatever. In that case, both of our solutions fail the general case. There could be multiple space separated tokens in name.

My suggestion now is to go with the regex approach, but do not anchor this to the beginning of the line. Instead use a regex that qualifies HO (or HT) with a minimum number of digits (could be 4,5,6, or above I used 8). That way, this field will not be confused with a name. HO could be a last name.

There was a suggestion to use a fixed field solution like unpack or substr. That can work well if there is one producer of the file. However, I often work with files that say "field X is 32 columns", but some guys put 30,31,32,33 columns in the output! As a defense, I write files like that exactly as spec'd, but allow more flexibility when reading files generated by others when I can.

As a PS: I prefer to assign directly to a variable rather than using the intermediate $1. I think the code "reads" better, but of course, your call on that.


In reply to Re^2: Find Not Working by Marshall
in thread Find Not Working by jlope043

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.