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

Re: split line

by dwm042 (Priest)
on Jul 28, 2008 at 21:14 UTC ( #700651=note: print w/replies, xml ) Need Help??


in reply to split line

When you're splitting on any whitespace, you want to use \s+ instead of " ". For example:

#!/usr/bin/perl -w use strict; my $line = " 0 10 9 4 1 0 0 0 2 2 1 1 0"; my @config = split /\s+/, $line; for (@config) { next unless $_ =~ /\w+/; print $_, "\n"; }
which has the output:

C:\Code>perl split_num.pl 0 10 9 4 1 0 0 0 2 2 1 1 0
Please note that with leading whitespace in $line, the first element returned by split won't contain any text, so you have to trap for that.

Replies are listed 'Best First'.
Re^2: split line
by ysth (Canon) on Jul 29, 2008 at 03:18 UTC
    When you're splitting on any whitespace, you want to use \s+ instead of " ".
    except when you want to use " ", which does just what you want, including saving the hassle of the empty first element:
    $ perl #!/usr/bin/perl -w use strict; my $line = " 0\t10\n 9 4 \r 1 0 0 \t0 2 2 1 1 0"; my @config = split " ", $line; for (@config) { print $_, "\n"; } __END__ 0 10 9 4 1 0 0 0 2 2 1 1 0
Re^2: split line
by lil_v (Sexton) on Jul 28, 2008 at 21:17 UTC
    thx alot!

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://700651]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others scrutinizing the Monastery: (4)
As of 2022-12-02 06:08 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Notices?