Gunnar Hjalmarsson <> wrote in comp.lang.perl.misc:
> Anno Siegel wrote:
> > Gunnar Hjalmarsson wrote:
> >> Joe Smith wrote:
> >>> Gunnar Hjalmarsson wrote:
> >>>>
> >>>> my $count = 0;
> >>>> $count ++ for split /\n/, $in{packageID};
> >>>
> >>> my $count = () = split /\n/,$in{packageID};
> >>
> >> my %in = ( packageID => "one\ntwo\nthree" );
> >>
> >> my $count = () = split /\n/, $in{packageID};
> >> print "$count\n";
> >>
> >> $count = 0;
> >> $count ++ for split /\n/, $in{packageID};
> >> print "$count\n";
> >>
> >> Outputs:
> >> 1
> >> 3
> >
> > That can be fixed:
> >
> > my $count = () = split /\n/,$in{packageID}, -1;
>
> Is it possible to figure out from the docs that LIMIT makes a
> difference in that respect?
Indirectly. The reason that we see $count = 1 is that split in list
context figures out how many elements are expected and, if there is
a bound, assumes a LIMIT of one more than that. (One more so that
the remainder of the string doesn't cling to the last element.)
Supplying an explicit, but ineffective, LIMIT of -1 stops that from
happening.
Actually, a LIMIT of 0 would be better because it behaves like no LIMIT
with respect to trailing empty fields. If the string is closed with a
line feed, as when it is read from a file, that makes a difference.
Did I mention split() is too clever for its own good?
> Wonder why I suggested the
>
> $count ++ for split /\n/, $in{packageID};
>
> solution?
Not at all.
my $count = @{ [ split /\n/,$in{packageID}]};
could also be used.
Anno