You have used the regex / / (match one space) as delimiter
for split. Unfortunatly the string in $line contains several
consecutive spaces, so perl does what it's told, split on one space. As an
example:
$line = "foo bar";
print ++$i, ". $_\n" for split / /, $line;
$line is assigned the string "foo..bar", where the
'.' is meant to be space (it's more visible). That would output:
1. foo
2.
3. bar
It 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)
But, what if your string looked something like this:
$line = " foo bar ";
Using \s+ as delimiter now would garble things up again.
It would return an extra element (which is empty) at the front.
How do we fix this then ? Well, the solution may look at bit counterintuitive
from what we have learned so far:
split " ", $line
Hm, wouldn't that just match one single space again ? Well, it should,
but it doesn't. This is a special case in perl, and if we apply
that to our test case we will get the following result:
1. foo
2. bar
Ah, just what we want (in almost all cases I would dare to say).
As an interesting note, consider the following:
perl -MO=Deparse -e 'split " "'
split(/\s+/, $_, 0);
Whereas
perl -MO=Deparse -e 'split /\s+/'
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 :-)
Autark
| [reply] [d/l] [select] |
split takes pretty robust regular expressions. Use the + quantifier to mean "at least one space":
split(/\s+/,$line); | [reply] [d/l] |
#!/usr/bin/perl -w
use strict;
my $line = "test1 22/11/1999 64 47 6% 263 38%
24 38% 47 73% 61 95% 63 98%";
my @stuff = split('\s+',$line);
my $test1 = join("|",$stuff[0],$stuff[1],$stuff[5]);
print "$test1\n";
J. J. Horner
Linux, Perl, Apache, Stronghold, Unix
jhorner@knoxlug.org http://www.knoxlug.org/
| [reply] [d/l] |