Jay Savage wrote:
> 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.

Yes, it appears that a list slice does not invoke the optimization:

$ perl -MO=Concise,-terse -e'
    ($x,$y) = split /:/, q[one:two:three:four:five:six:seven:eight]
'
LISTOP (0x818a978) leave [1]
    OP (0x818b0c8) enter
    COP (0x8170958) nextstate
    BINOP (0x818aa10) aassign [4]
        UNOP (0x818aa78) null [141]
            OP (0x824e340) pushmark
            LISTOP (0x818ab80) split [3]
                PMOP (0x827fca8) pushre
                SVOP (0x818a9c0) const [5] PV (0x81877b8)
"one:two:three:four:five:six:seven:eight"
                SVOP (0x818a240) const [6] IV (0x816cd04) 3
        UNOP (0x818ace0) null [141]
            OP (0x8170900) pushmark
            UNOP (0x818ac88) null [15]
                PADOP (0x818abb8) gvsv  GV (0x8187794) *x
            UNOP (0x81708a8) null [15]
                PADOP (0x818a9f0) gvsv  GV (0x8187710) *y
-e syntax OK
$ perl -MO=Concise,-terse -e'
    ($x,$y) = ( split /:/, q[one:two:three:four:five:six:seven:eight] )[0,1]
'
LISTOP (0x818c4b8) leave [1]
    OP (0x824e038) enter
    COP (0x8170958) nextstate
    BINOP (0x8170860) aassign [4]
        UNOP (0x818a988) null [141]
            OP (0x824e058) pushmark
            BINOP (0x818aa20) lslice
                UNOP (0x818ab90) null [141]
                    OP (0x824d688) pushmark
                    SVOP (0x824e358) const [5] IV (0x8187798) 0
                    SVOP (0x818b0d8) const [6] IV (0x816cce0) 1
                UNOP (0x818aa88) null [141]
                    OP (0x824e078) pushmark
                    LISTOP (0x8205c78) split [3]
                        PMOP (0x8170818) pushre
                        SVOP (0x818a9d0) const [7] PV (0x81877c8)
"one:two:three:four:five:six:seven:eight"
                        SVOP (0x818a250) const [8] IV (0x8187738) 0
        UNOP (0x818acf0) null [141]
            OP (0x8170900) pushmark
            UNOP (0x818ac98) null [15]
                PADOP (0x818abc8) gvsv  GV (0x81877a4) *x
            UNOP (0x81708a8) null [15]
                PADOP (0x818aa00) gvsv  GV (0x8187720) *y
-e syntax OK


You will notice that the third argument to split using the list slice is 0.



John
-- 
Perl isn't a toolbox, but a small machine shop where you can special-order
certain sorts of tools at low cost and in short order.       -- Larry Wall

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


Reply via email to