friedo and jhourcle are right about this being a job for split, but the problem of capturing matches from a regex is common enough that I think a brief elaboration on friedo's answer would be worthwhile.

Even if you had written

my ( $text ) = m/\|(.*?\|){5}(.*?)\|(.*?\|){3}(SNOMEDCT)/;
thereby evaluating the RHS in a list context, you still would have ended up with the wrong text in $text, namely whatever was matched by the first last (.*?\|). So if you didn't change the regex you'd have to capture all the submatches in an array and figure out which slot in the array has the submatch of interest.

I think it is simpler to use ?: to disable capture in all but those parens for which you actually want to capture something (this is a good programming habit too, because it minimizes the effect that an edit to the regexp will have on the indices of the captured matches).

For example, in your regex only the second set of parens is capturing something of interest, threfore the capture can be disabled in all the others; actually the last set of parens (around SNOMEDCT) is required neither for grouping nor capturing, so you can eliminate it altogether:

my ( $text ) = m/\|(?:.*?\|){5}(.*?)\|(?:.*?\|){3}SNOMEDCT/;

the lowliest monk


In reply to Re^3: unique fields in bar delimited record by tlm
in thread unique fields in bar delimited record by jjohhn

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.