Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.perl.misc > #8844
| Newsgroups | comp.lang.perl.misc |
|---|---|
| Subject | Re: No more than N element of an array |
| References | <ksrg9b$gv9$1@reader1.panix.com> |
| From | Ben Morrow <ben@morrow.me.uk> |
| Organization | morrow.me.uk |
| Date | 2013-07-26 05:12 +0100 |
| Message-ID | <4j99ca-t92.ln1@anubis.morrow.me.uk> (permalink) |
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
>
> @results = @results[0 .. (MAXSEARCHRESULTS - 1,
> $#results)[MAXSEARCHRESULTS - 1 > $#results ]];
>
> He felt it was "awesome". I thought it was way too cute.
I'm not sure 'cute' is the word I'd use...
> Of course there's
> @results = @results[0 .. (MAXSEARCHRESULTS - 1 > $#results ? $#results :
> MAXSEARCHRESULTS - 1)];
> It has the same number of uses of variables, just in a different order.
>
> I considered plain @results[0 .. MAXSEARCHRESULTS]. It works if the
> array is longer than the limit, but pads with undef if shorter:
>
> $ perl -e 'use strict; use warnings; my @a = (0, 1, 2, 3, 4, 5, 6); @a =
> @a[0..10]; print join(",", @a), "\n"'
> Use of uninitialized value $a[7] in join or string at -e line 1.
> Use of uninitialized value $a[8] in join or string at -e line 1.
> Use of uninitialized value $a[9] in join or string at -e line 1.
> Use of uninitialized value $a[10] in join or string at -e line 1.
> 0,1,2,3,4,5,6,,,,
>
> I thought of splice.
>
> $ perl -e 'use strict; use warnings; my @a = (0, 1, 2, 3, 4, 5, 6);
> splice @a, 3; print join(",", @a), "\n"'
> 0,1,2
> $ perl -e 'use strict; use warnings; my @a = (0, 1, 2, 3, 4, 5, 6);
> splice @a, 10; print join(",", @a), "\n"'
> splice() offset past end of array at -e line 1.
You can kill this warning with
no warnings "misc";
> 0,1,2,3,4,5,6
> $ perl -e 'print $], "\n"'
> 5.014002
>
> But that's on my ISP. On our office machines, no warning.
>
> $ perl -e 'use strict; use warnings; my @a = (0, 1, 2, 3, 4, 5, 6);
> splice @a, 3; print join(",", @a), "\n"'
> 0,1,2
> $ perl -e 'use strict; use warnings; my @a = (0, 1, 2, 3, 4, 5, 6);
> splice @a, 10; print join(",", @a), "\n"'
> 0,1,2,3,4,5,6
> $ perl -e 'print $], "\n"'
> 5.016002
> $ perlfunc splice | cat
> ...
> If OFFSET is past the end of the array,
> Perl issues a warning, and splices at the end of the array.
> ...
> So is splice likely to ever output such a warning again, so the 5.16
> documentation is simply outdated? Or was that just an error in 5.16
> and the warning now comes out in 5.18?
The behaviour changed in 5.16. In 5.14 and before, both these cases
warned:
splice @a, 10, 3;
splice @a, 10;
but for 5.16 it was decided that the second was actually entirely
unambiguous, so only the first case warns.
> To protect against that,
> splice @results, MAXSEARCHRESULTS if @results > MAXSEARCHRESULTS;
> Doable (warning: that's untested code). But it's not as pretty.
>
> Is there a cleaner way?
Perhaps
@results = splice @results, 0, MAXSEARCHRESULTS;
Ben
Back to comp.lang.perl.misc | Previous | Next — Previous in thread | Next in thread | Find similar
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