in reply to replace and remove spaces

Try using this code

$var = "12 Ali Engineer B889 Middle town USA"; $var =~ s/(\S*)\s*(\S*)\s*(\S*)\s*(\S+)*/$1,$2,$3,$4/; $var =~ s/\s+//g; print $var . "\n";

Replies are listed 'Best First'.
Re^2: replace and remove spaces
by suhailck (Friar) on Feb 21, 2011 at 08:27 UTC
    Another approach,

    perl -le '$var = "12 Ali Engineer B889 Middle town USA"; print join ",",map {s/\s+//g;$_} split /\s+/,$var,4' 12,Ali,Engineer,B889MiddletownUSA