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

Hi Frenz,

I need to remove the white spaces from a string and print just the string. Below is the code:
$line = " c1, c2, s1,s2, c3"; @comboSeq = split(/\,/,$line); foreach(@comboSeq) { writeToFile("file.xml","\"$_\"\n"); }

Output:

" c1"

" c2"

" s1"

"s2"

" c3"

I don wan the preceding spaces.

All i need is

"c1"

"s1" and so on.

Plz help.

--NewBee

20091130 Janitored by Corion: Restored title and content

Replies are listed 'Best First'.
Re: delete space
by keszler (Priest) on Nov 30, 2009 at 10:29 UTC
    $ perl -we ' print "--$_--\n" for grep $_, split /[ ,]+/, " c1, c2, s1, s2, c3 "; ' --c1-- --c2-- --s1-- --s2-- --c3--
    Split on [ ,]+ so that split eats the spaces as well as the commas. The grep eliminates the first empty ("") element.
      That will cause "c1, c2a c2b, c3" to return four elements instead of three.
      split /, /
      or
      split /\s*,\s*/
      is more appropriate.

      s/\s+//g

      This solved the problem. thanks all
Re: delete space
by ww (Archbishop) on Nov 30, 2009 at 11:16 UTC
    Precision in stating your problem is helpful.
    I need to remove the white spaces from a string and print just the string.
    and
    All i need is
    "c1"
    "s1" and so on.

    Which is it? Remove just the spaces or remove both spaces and commas? Your regex suggests you do want to remove the commas but your narrative says otherwise.

Re: delete space
by Fox (Pilgrim) on Nov 30, 2009 at 11:47 UTC
    now if you want to remove the whitespaces after the split you may want to see: TRIM in Perl?