in reply to strip multiple spaces
$line is assigned the string "foo..bar", where the '.' is meant to be space (it's more visible). That would output:$line = "foo bar"; print ++$i, ". $_\n" for split / /, $line;
1. foo 2. 3. barIt prints out three lines, one which is empty (because there is nothing between the two spaces). So we have to deal with more than one whitespace. We could do this: split /\s+/, $line Now, perl will look for 1 or more spaces when trying to figure out where to split - and in your example that would probabely work out just fine. (don't use \s* as delimiter as that would match everywhere)
1. foo 2. barAh, just what we want (in almost all cases I would dare to say). As an interesting note, consider the following:
Whereasperl -MO=Deparse -e 'split " "' split(/\s+/, $_, 0);
generates the exact same output - but we know that the semantics is not the same. But as long as perl does The Right Thing(TM), I'm happy :-)perl -MO=Deparse -e 'split /\s+/' split(/\s+/, $_, 0);
Autark
|
|---|