T
Tim McDaniel
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.
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.
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?
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?
(As it happens, he now has to check the limit in an if anyway for
another purpose, so he knows that @results[0 .. MAXSEARCHRESULTS - 1]
won't go past the end, so the question is now moot. I'm still
curious.)
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.
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.
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?
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?
(As it happens, he now has to check the limit in an if anyway for
another purpose, so he knows that @results[0 .. MAXSEARCHRESULTS - 1]
won't go past the end, so the question is now moot. I'm still
curious.)