On 9/20/2005 11:16 PM Christopher Spears wrote:

I've been learning about data structures by reading
the Programming Perl book.  Here is a code snippet:

while ($line = <>) {

# If $line = "abcd: efgh: ijkl";

    ($who, $rest) = split /:\s*/, $line, 2;

# then $who = "abcd" and $rest = "efgh: ijkl";

    @fields = split ' ', $rest;
    $HoA{$who} = [ @fields ];
}

The part that confuses me is:

($who, $rest) = split /:\s*/, $line, 2;

I understand that it takes the input and splits it
into two parts along the regular expression, but I
can't figure out what the 2 means.

perldoc -f split

split /PATTERN/,EXPR,LIMIT

[...]

If LIMIT is specified and positive, splits into no more than that
many fields (though it may split into fewer).  If LIMIT is unspecified
or zero, trailing null fields are stripped (which potential users
of C<pop()> would do well to remember).  If LIMIT is negative, it is
treated as if an arbitrarily large LIMIT had been specified.

[...]

The LIMIT parameter can be used to split a line partially

    ($login, $passwd, $remainder) = split(/:/, $_, 3);

When assigning to a list, if LIMIT is omitted, Perl supplies a LIMIT
one larger than the number of variables in the list, to avoid
unnecessary work.  For the list above LIMIT would have been 4 by
default.  In time critical applications it behooves you not to split
into more fields than you really need.

[...]

--
Ankur

--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>


Reply via email to