Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change

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
[GrandFather]: It's either lines that your browser won't split (no white space) or nodes with <pre> tags which ought to be code tags
[GrandFather]: I haven't noticed the problem for a while, but it can be hard to find the nasty node. If you do find it, consider it and with luck a janitor will fix it

How do I use this? | Other CB clients
Other Users?
Others lurking in the Monastery: (5)
As of 2017-05-24 01:01 GMT
Find Nodes?
    Voting Booth?