Beefy Boxes and Bandwidth Generously Provided by pair Networks Russ
Do you know where your variables are?
 
PerlMonks  

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

Edited by Chady -- added code tags.

Comment on spliting new lines and spaces
Download Code
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,
    Kris

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://351497]
Approved by Roger
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others chanting in the Monastery: (8)
As of 2014-04-17 05:36 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    April first is:







    Results (439 votes), past polls