With match global, you can just assign the authors to an array of @authors (no while loop needed).

There appear to be some special cases for formatting..0,1,2,3,more authors. Some simple "if" logic could solve that.

The more general situation would be names like: Bob Smith, MD; Porterhouse, A. B.; Freddie, Jr. or some such name with commas in it. Often ";" is used instead of commas so that it is possible to re-parse this concatenated line back into the original names. You should consider the ramifications (if any) of creating a string that is difficult for a program to re-parse.

Update: To the best of my knowledge, the use of ";" as a separator when one or more names contain commas, is proper English grammar. I would suggest scalar grep.  if (grep{/,/}@authors){ #use ';' instead of ',' for the separator.}

#!/usr/bin/perl -w use strict; my $str = '<P_contrib-author>Mariel Miller</P_contrib-author>,<P_contr +ib-author>Allyson Fiona Hadwin</P_contrib-author>,<P_contrib-author>J +enna Cambria</P_contrib-author>'; my @authors = ($str =~ /<P_contrib-author>\s*(.*?)\s*<\/P_contrib-author>/g); if (@authors >= 3) { print join (", ", @authors[0..@authors-2]), " and $authors[@authors-1]\n"; } elsif (@authors == 2) { print "$authors[0] and $authors[1]\n"; } else { print "$authors[0]\n"; }

In reply to Re: string concatenation by Marshall
in thread string concatenation by maha

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.