On 3/3/07, John W. Krahn <[EMAIL PROTECTED]> wrote:
Jay Savage wrote:
> On 3/2/07, Robert Boone <[EMAIL PROTECTED]> wrote:
>> I think this is all you do:
>>
>> $piid = (split(/\t/, $row))[0];
>
> Split also takes an optional limit that keeps it from splitting the
> string into more than n parts. This keeps spilt from performing
> useless operations when you only want the first n-1 items, or when you
> want to lump all the items >= n into a single lump:
>
> $piid = (split(/\t/,$row,2)[0];
>
> Most of the time it probably doesn't matter, but adding a limit will
> be markedly more efficient if $row is particularly long or you are
> looping through an extremely long list of rows.
>
> As always, see perldoc -f split for the details.
perldoc -f split
[ snip ]
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, or zero, 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.
The limit is supplied automagically if the size of the list is know at compile
time like in your example above so using the limit argument is superfluous.
I read the doc to say that, given a list of size n, perl will perform
n + 1 splits by default. limit has diminishing returns as n increases,
but for a list of length one, not supplying a limit means double the
work, since (n + 1) = 2n when n is 1.
Furthermore, it's not clear to me what the default limit is in the
case of a slice. Consider
$piid = (split(/\t/,$row)[-1];
$piid = (split(/\t/,$row)[4];
It seems to me that in the case of a slice, split must split the
entire string, and then return the appropriate element. Wanting a
single element and wanting the first element are two differnt things.
Maybe the compiler optimizes for the case of a slice with index [0]?
It may, but it's not obvious to me from the docs that it does.
Best,
-- jay
--------------------------------------------------
This email and attachment(s): [ ] blogable; [ x ] ask first; [ ]
private and confidential
daggerquill [at] gmail [dot] com
http://www.tuaw.com http://www.downloadsquad.com http://www.engatiki.org
values of β will give rise to dom!