On 3/3/07, Rob Dixon <[EMAIL PROTECTED]> wrote:
Jay Savage wrote:
[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.

Almost. It will perform n splits, resulting in n+1 pieces. The document says
earlier on:

     If LIMIT is specified and positive, it represents the maximum
     number of fields the EXPR will be split into, though the actual
     number of fields returned depends on the number of times PATTERN
     matches within EXPR.

 > 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.

Not sure what you mean here. The returns diminish as n approaches the number
of separators in the string, which is - sort of - as n increases. But when the
list has length one the default behaviour is to split into two pieces. What
work is doubled there? Were you thinking it would split twice into three
pieces? Why?


Fencepost error. The second error follows from the first. I realized
it almost as soon as I hit send, but I was on the way out the door. As
I was writing, I was thinking that limit was the number of splits
performed and limit 1 would give the desired result. But of course
that's not the case. The limit is the number of items returned, not
the number of splits performed, and limit 1 returns the entire input
string.

[snip]


Instead of supplying an explicit value for the limit, the way to get the
default behaviour to work in this instance is to write

   ($piid) = split /\t/, $row;

which would be optimised to

   ($piid) = split /\t/, $row, 2;


That's almost certainly the best solution to grabbing the first item
out of delimited list with split.

But it only works for the first item and isn't a useful perlish idiom
for replicating the behavior of Python's slice notation (which seems
to be basically a copy of Perl's), which was what I understood OP's
question as. And for a slice, I would give a useful limit where
possible, since the deafult limit in that case is 0.

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!

Reply via email to