Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

Splicing an array.

by Anonymous Monk
on Aug 13, 2000 at 06:18 UTC ( [id://27674]=perlquestion: print w/replies, xml ) Need Help??

Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Hello.

I am trying to splice an array through a variable that gets processed/cleaned. However, I have found out that the following pieces of code are not equivalent.

print @array[2,3..7];
is not equal to...but gets a runtime error
my $splice_str = '2,3..7'; print @array[$splice_str];
Which returns:
Argument "2,3..7" isn't numeric in array slice at ...

Any ideas?

Replies are listed 'Best First'.
RE: Splicing an array.
by nuance (Hermit) on Aug 13, 2000 at 06:37 UTC
    You are trying to take an array slice with a string, the punctuation prevents it being converted to a number.

    you could do this:

    Updated to fix silly typo as pointed out by Kozz below

    my @splice_array = (2, 3..7); print @array[@splice_array];
    the problem is that you want the "string" to be expanded to a list and that won't happen with your current method. If you really want to use the string, then you have to eval it, as this will cause it be be expanded into a list, thus:
    my $splice_str = "2,3..7"; print @array[eval $splice_str];
    but that's probably grossly inefficient.

    Nuance

      Careful... your advice isn't 100% kosher... if I do exactly what you have listed I get a warning:

      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:

      #!/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, });
      and the results... were... well, very within what we would have expected them to be :)
      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)
      so, the array slice version is only a factor of 100 faster :)
      Just fixing a typo in nuance's first solution. I think he meant
      my @splice_array = (2, 3..7); print @array[@splice_array];
Re: Splicing an array.
by reptile (Monk) on Aug 14, 2000 at 02:44 UTC

    Well, first of all, "2,3..7" is the same as "2..7", but ignoring that, this works:

    @array[2,(3..7)];

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://27674]
Approved by root
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others avoiding work at the Monastery: (5)
As of 2024-03-29 00:03 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found