I have read and tested my code but I don't quite understand the output in 2 cases, will you explain?
I get two different output here from a single regex expression and the difference is a space between what I assigned to be my $1 and my $2 captures
I have googled this and read thru perl tutorials and I have found no real explanation that can answer my simple question, perhaps you can help?
s/^(.*)+([^ ]+)$/$2, $1/; #$1 is anything that starts is 1st name so / +^(.*) and $2=anything else after
this worked, please note the space between $1 and when I try to capture the $2 value...I only found this worked because I fat fingered it while looking for the reason my first try didn't work..,I ended up doing it differently to capture right last name but I still have need to get answer on this
s/^(.*) +([^ ]+)$/$2, $1/;
entire test code that gives me an error for demo
use strict; my @cognome = last_name_first("leonardo Da Vincy","Raffaello da Urbino +"); print join(";",@cognome); sub last_name_first { my@names=@_; foreach(@names){ s/^(.*)+([^ ]+)$/$2, $1/; #$1 anything that starts is 1st name a +nd $2=anything else after..FAIL! #s/^(.*) +([^ ]+)$/$2, $1/; #works... space seems to do it,why? } return @names; }
In reply to need help with explaining the output by perlynewby
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |