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
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |