Scalar value @array[ eval $splice_str ] better written as $array[ eval $splice_str ] at ./foo.pl line 17.
Which is never really a good thing... we want to stay away from error messages as best as we can :) The best solution then is to wrap the eval in ( ), I wrote up some quick code to benchmark both situations:
and the results... were... well, very within what we would have expected them to be :)#!/usr/bin/perl -w use vars qw/@splice_array $splice_str @array/; use strict; use Benchmark; my @splice_array = (2, 3..7); my $splice_str = "2, 3..7"; my @array = (1 .. 10); sub with_array { return @array[@splice_array]; } sub with_string { return @array[ (eval $splice_str) ]; } timethese (-5, { "array" => \&with_array, "string" => \&with_string, });
so, the array slice version is only a factor of 100 faster :)Benchmark: running array, string, each for at least 5 CPU seconds... array: 0 wallclock secs ( 5.02 usr + 0.00 sys = 5.02 CPU) @ 54 +8512.95/s (n=2753535) string: 6 wallclock secs ( 5.29 usr + 0.00 sys = 5.29 CPU) @ 56 +98.87/s (n=30147)
In reply to RE: RE: Splicing an array.
by eduardo
in thread Splicing an array.
by Anonymous Monk
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |