Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.perl.misc > #8849

Re: No more than N element of an array

From Charles DeRykus <derykus@gmail.com>
Newsgroups comp.lang.perl.misc
Subject Re: No more than N element of an array
Date 2013-07-26 02:19 -0700
Organization Aioe.org NNTP Server
Message-ID <kstev5$71k$1@speranza.aioe.org> (permalink)
References <ksrg9b$gv9$1@reader1.panix.com> <4j99ca-t92.ln1@anubis.morrow.me.uk> <kst3q6$91q$1@speranza.aioe.org> <ctm9ca-mc3.ln1@anubis.morrow.me.uk>

Show all headers | View raw


On 7/26/2013 1:00 AM, Ben Morrow wrote:
>
> Quoth Charles DeRykus <derykus@gmail.com>:
>> On 7/25/2013 9:12 PM, Ben Morrow wrote:
>>>
>>> Quoth tmcd@panix.com:
>>>> A cow-orker is coding something: he gets an array of results, but
>>>> wants to take no more than the first 1000 elements.  His suggestion was
> [...]
>>>> Is there a cleaner way?
>>>
>>> Perhaps
>>>
>>>       @results = splice @results, 0, MAXSEARCHRESULTS;
>>
>> If fewer strokes were to factor into cleanliness, even:
>>
>>       @results = @results[0..MAXSEARCHRESULTS];
>
> Tim already pointed out that this returns extraneous undefs if @results
> is too short.

Sigh, I missed it.

You could tweak it via with  [0..min($#results,MAXSEARCRESULTS)] but, 
aside from the purist's objection of adding a module,  it should be DOA 
anyway with its inefficiency (I'm guessing that it does more copying so 
is slower).

>
>> But, as you grow array size and MAXSEARCHRESULTS,  it gets filthy slow...
>>
>> Setting $#results = MAXSEARCHRESULTS undoubtedly comes out of the wash
>> purest and fastest.
>
> It's probably easiest, though turning off the warning and using Tim's
>
>      splice @results, MAXSEARCHRESULTS - 1;
>
> is probably better, on balance.

Turning off a warning category seems slightly unclean to me... even 
though it's only because of the earlier version.


  Using $#ary as an lvalue has some
> permanent side-effects on the array; you can see them with Devel::Peek.
>
> [The side-effects are to do with the fact that $#ary is a scalar lvalue
> and \$#ary should return a ref to the same scalar every time, so we need
> an actual permanent scalar somewhere, which turns out to get stored in
> the array's magic.]
>

Interesting. IIUC any real downside other than the extra storage in 
magic?  I thought I remembered truncating via $#ary doesn't return the 
memory to the process unlike undef @ary.

-- 
Charles DeRykus

Back to comp.lang.perl.misc | Previous | NextPrevious in thread | Next in thread | Find similar


Thread

No more than N element of an array tmcd@panix.com (Tim McDaniel) - 2013-07-25 15:29 +0000
  Re: No more than N element of an array Rainer Weikusat <rweikusat@mssgmbh.com> - 2013-07-25 16:38 +0100
    Re: No more than N element of an array Rainer Weikusat <rweikusat@mssgmbh.com> - 2013-07-25 16:44 +0100
  Re: No more than N element of an array Ben Morrow <ben@morrow.me.uk> - 2013-07-26 05:12 +0100
    Re: No more than N element of an array Charles DeRykus <derykus@gmail.com> - 2013-07-25 23:08 -0700
      Re: No more than N element of an array Ben Morrow <ben@morrow.me.uk> - 2013-07-26 09:00 +0100
        Re: No more than N element of an array Charles DeRykus <derykus@gmail.com> - 2013-07-26 02:19 -0700
          Re: No more than N element of an array "Peter J. Holzer" <hjp-usenet3@hjp.at> - 2013-07-26 11:32 +0200
            Re: No more than N element of an array Charles DeRykus <derykus@gmail.com> - 2013-07-26 03:38 -0700
          Re: No more than N element of an array Ben Morrow <ben@morrow.me.uk> - 2013-07-26 11:26 +0100
          Re: No more than N element of an array tmcd@panix.com (Tim McDaniel) - 2013-07-26 14:27 +0000
            min (), and Perl modules Ivan Shmakov <oneingray@gmail.com> - 2013-07-26 15:16 +0000
              Re: min (), and Perl modules tmcd@panix.com (Tim McDaniel) - 2013-07-26 16:17 +0000
            Re: No more than N element of an array Ben Morrow <ben@morrow.me.uk> - 2013-07-26 16:39 +0100
        Re: No more than N element of an array Rainer Weikusat <rweikusat@mssgmbh.com> - 2013-07-26 16:38 +0100
          Re: No more than N element of an array Ben Morrow <ben@morrow.me.uk> - 2013-07-26 19:23 +0100
            Re: No more than N element of an array Rainer Weikusat <rweikusat@mssgmbh.com> - 2013-07-30 17:52 +0100
              Re: No more than N element of an array Charles DeRykus <derykus@gmail.com> - 2013-07-30 22:10 -0700

csiph-web