On 25/03/2012 10:23, lina wrote: > > For > > Rissman, R.; Poon, W. W.; Blurton, M.; Oddo, S.; Torp, R. > > I wish to get the output as > > R. Risman and W. W. Pooon and M. Blurtoon and S. Oddoo and R. Toorp, R. > > Here is the code I came up so far, which works partially > > > #!/usr/bin/env perl > > use strict; > use warnings; > use 5.012; > > my $str=$ARGV[0]; > > > my @b = split /;/, $str; > > foreach (@b){ > if (/([A-Z][a-z]+)\,[ ]([A-Z]\.+)/){ > print $2," ", $1, " and "; > } > } > > R. Risman and W. Pooon and M. Blurtoon and S. Oddoo and R. Toorp and > > which missed the W. W. Pooon.
You program fails because of the regex for the initials. You have /[A-Z]\.+/ which matches one capital letter followed by one or more dots. The dot needs to be inside the character class, and you need to put a space in there to allow for "W. W.". This would work if (/([A-Z][a-z]+)\,[ ]([A-Z. ]+)/) { : } But you could more easily use split again on the comma, like this my @name = split /,\s*/; print $name[1], " ", $name[0], " and "; but you still have a trailing "and" on the output. You could fix that and write it more concisely as use strict; use warnings; my $names = 'Rissman, R.; Poon, W. W.; Blurton, M.; Oddo, S.; Torp, R.'; print join ' and ', map { join ' ', reverse split /,\s*/; } split /;\s*/, $names; **OUTPUT** R. Rissman and W. W. Poon and M. Blurton and S. Oddo and R. Torp HTH, Rob -- To unsubscribe, e-mail: beginners-unsubscr...@perl.org For additional commands, e-mail: beginners-h...@perl.org http://learn.perl.org/