Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris

spliting new lines and spaces

by bory (Beadle)
on May 07, 2004 at 15:49 UTC ( #351497=perlquestion: print w/replies, xml ) Need Help??
bory has asked for the wisdom of the Perl Monks concerning the following question:

Hi if I have an array like this (with lots of spaces and new lines):
my @test=("aaa bbbbb cccc"); How can I transform it in: my @test=("aaabbbccc");

Edited by Chady -- added code tags.

Replies are listed 'Best First'.
Re: spliting new lines and spaces
by Roger (Parson) on May 07, 2004 at 15:53 UTC
    Do you mean you want to eliminate spaces and new lines?
    use Data::Dumper; my @test=("aaa bbbbb cccc"); my @test = map { s/\s//g; $_ } @test; print Dumper(\@test);

    And the output is as expected:
    $VAR1 = [ 'aaabbbbbcccc' ];

Re: spliting new lines and spaces
by krusty (Hermit) on May 07, 2004 at 15:57 UTC
    The sample code you have given only contains one element in the array. Is that what you really meant?

    The following code should work to get rid of any whitespace characters (tab, newline, form feed, literal space, carraige return) in the elements of your array:
    foreach (@test){ s/\s+//g; }
    Good luck,

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://351497]
Approved by Roger
[chacham]: I'm sure this is old, but i just came across it: If you put a million monkeys at a million keyboards, one of them will eventually write a Java program. The rest of them will write Perl programs.
[LanX]: lol
[Corion]: Woo hoo! A new Auralnauts episode!
LanX mean unfair and funny
[LanX]: but most java code looks like from one million monkey using eclipse. ..

How do I use this? | Other CB clients
Other Users?
Others romping around the Monastery: (13)
As of 2018-04-23 16:03 GMT
Find Nodes?
    Voting Booth?