Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid

Re: split line

by gaal (Parson)
on Jul 28, 2008 at 21:11 UTC ( #700645=note: print w/replies, xml ) Need Help??

in reply to split line

You're trying to split on a separator that is literally a space between quote marks. Instead, you want to split on any amount of consecutive whitespace:

my @config = split /\s+/, $line

Note that you have leading spaces in your string; you may want to strip those out before splitting.

Replies are listed 'Best First'.
Re^2: split line
by lil_v (Sexton) on Jul 28, 2008 at 21:15 UTC
    this works except for the first element, the first element still gets blank. Is there any way to chomp the begining?

      dwm042's answer below does this but there's a shortcut specifically for this kind of thing. Try this:

      my $line = " 0 10 9 4 1 0 0 0 2 2 1 1 0"; my @config = grep /\S/, split / /, $line; # or even my @config = grep /\A\d+\z/, split / /, $line; print join(", ", @config), "\n";

      The second will only pass through numbers (well, positive integers and zero) so something like "6a" will be skipped.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://700645]
[Corion]: Oooh - what a nice idea - QoS throttling on AWS, test enviroment runs fast, production also, until it doesn't
[Corion]: But then, you can just pay to make it fast again. Be a shame if it were to slow down, wouldn't it?

How do I use this? | Other CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (7)
As of 2017-07-26 18:42 GMT
Find Nodes?
    Voting Booth?
    I came, I saw, I ...

    Results (400 votes). Check out past polls.