sufi has asked for the wisdom of the Perl Monks concerning the following question:

This node falls below the community's threshold of quality. You may see it by logging in.

Replies are listed 'Best First'.
Re: replace and remove spaces
by GrandFather (Saint) on Feb 21, 2011 at 04:34 UTC

    And?

    What do you expect from us? We can't see the code you've tried already and you haven't told us where you are having trouble so: what do you expect from us?

    You haven't even told us what the criteria are for determining when to replace spaces and when to remove them. Maybe you need a better problem description and to show that you've actually tried to solve the problem yourself?

    True laziness is hard work
Re: replace and remove spaces
by aantonyselvam (Beadle) on Feb 21, 2011 at 08:10 UTC

    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";
      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