Dmitry Epstein (
[email protected]) wrote:
: (e-mail address removed) (Malcolm Dew-Jones) wrote in
:
: > Dmitry Epstein (
[email protected]) wrote:
: >: : >: > Dmitry Epstein wrote:
: >: >> Here is a code snippet:
: >: >>
: >: >> while ($line = <F>) {
: >: >> my ($node1, $node2, $node3) = split ' ', $line;
: >: >> last unless defined $node3;
: >: >> ...
: >: >> }
: >: >>
: >: >> The idea here was that the loop stops if the line was
: >: >> split into fewer than 3 values. It doesn't work however:
: >: >> when the line has only 2 values, the variable $node3 is
: >: >> initialized with zero instead of remaining undefined. How
: >: >> come?
: >: >>
: >: >> (I have since changed the code to use an array instead of
: >: >> a list.)
: >: >>
: >: >
: >: > Since the pattern ' ' is split()ing on whitespace, the
: >: > newline on each line results in an empty trailing field
: >: > ('', which is a defined value). So you need to get rid of
: >: > the newline on each line:
: >: >
: >: > use strict;
: >: > use warnings;
: >: >
: >: > while (my $line = <DATA>) {
: >: > chomp $line;
: >: > my ($node1, $node2, $node3) = split ' ', $line;
: >: > last unless defined $node3;
: >: > print join(" ", $node1, $node2, $node3), "\n"
: >: > }
: >: >
: >: > __DATA__
: >: > a s d
: >: > a s
: >: >
: >: > HTH- keith
: >
: >: I don't think so. Split on ' ' is supposed to "chomp" any
: >: trailing whitespace. In fact, if I use an array to store the
: >: value of split instead of a list of scalars, the array does
: >: not receive a null value in the end.
: >
: > Not exactly. The "default" behaviour is to drop empty
: > trailing fields, but what does "default" mean?
: >
: > perldoc -f split also states that assigning to a _list_ (which
: > is what you have) creates an implicit limit on the number of
: > fields to be extracted.
: >
: > So I am guessing that "default" is refering to the "limit",
: > but since you have implicitly defined a limit then you don't
: > get the default behaviour.
: >
: > Instead you get the first three fields, the third of which is
: > the empty string "found" at the end.
: >
: >
: > "first-item second-item \n"
: > 1111111111 22222222222 3 (3rd is 0 chars long)
: Heh, I didn't say my example was the "default" case (split with no
: arguments, as I understand it), now did I?
This is what the
: docs say:
: As a special case, specifying a PATTERN of space (' ') will split
: on white space just as split with no arguments does.
The _Default_ behaviour to which I refered, and which I quote below, is in
the first paragraph of the perl I am using today ( v5.6.1 built for
MSWin32-x86-multi-thread )
=item split
... By default,
empty leading fields are preserved, and empty trailing ones are
deleted.
So, what will split do with the trailing empty fields?
: There is a bit more on that case in the docs, but nowhere does it
: say that specifying a limit, whether implicitly or explicitly,
: somehow abrogates the standard behaviour of split on ' '.
No, but by the same token, the docs for the special case of ' ' do not
indicate what happens to the trailing fields, only ever the leading
fields. Since no mention is made of the trailing fields, and since you do
not specify the number of fields, therefore I assume the indicated default
behaviour for trailing fields applies.