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

I'm trying to use splice to obtain an array that is the subset of another array but something isn't working. The following code, returns a value of 67 for the pair of arguments of splice (1, -3) rather than the array 34 67. Why?

#!/usr/bin/perl my @a = (99999, 34,67,976,432,99999); my $length = scalar(@a); for ($counter = 0; $counter<=$length; $counter++){ my $max = max(-3 + $counter, 1); print "$max\n"; my $second_counter = -($length - $counter); print "$second_counter\n"; my @worker = splice @a, $max, $second_counter; print "@worker\n"; }

Replies are listed 'Best First'.
Re: Sub-setting an Array
by 1nickt (Canon) on Sep 05, 2015 at 00:02 UTC

    Hi kgherman,

    The following code, returns a value of 67 . . .

    This code doesn't compile. You should always use strict; and use warnings;, even in example code, then you won't forget to include something like the package that holds your max() ...

    In order to get the results you want from the array you have, you need:

    splice(@array,1,2);
    . . . I'm not sure what the rest of your code is for; please feel free to explain what you are really trying to do. Maybe you were trying to do something like this?
    #!/usr/bin/perl use strict; use warnings; my @array = ( 99999, 34, 67, 976, 432, 99999 ); my $elements = @array; for ( my $offset = 0; $offset < $elements; $offset++ ) { for ( my $length = 0; $length <= $elements; $length++ ) { my @to_splice = @array; my @spliced = splice @to_splice, $offset, $length; print "splice(\@array, $offset, $length) : @spliced\n"; } }

    Upate: added example

    The way forward always starts with a minimal test.
      Thank you for your feedback; I'm trying to make a for loop that will return subsets of length 3 of the original array, starting from the beginning of the array. So, when i start from the 1st element of the original array, since there isn't 2 elements before it, the for loop should only return that one element; when I move to the second iteration, since I'm at the second element of the original array there still isn't 3 elements before it so the code should return an array with the first two elements, and so forth. Does this make sense?

        That's an odd way to get subsets of three elements. You can't grab them three at a time and maybe the end one has fewer than three elements? As in the example in the docs for splice?

        sub nary_print { my $n = shift; while (my @next_n = splice @_, 0, $n) { say join q{ -- }, @next_n; } } nary_print(3, qw(a b c d e f g h)); # prints: # a -- b -- c # d -- e -- f # g -- h

        The way forward always starts with a minimal test.
Re: Sub-setting an Array
by stevieb (Canon) on Sep 04, 2015 at 23:59 UTC

    Hi kgherman,

    It is considered good etiquette when you cross-post amongst many sites to state you've done so, so that the Monks aren't spending time on solving your issue when it may have already been solved somewhere else.

    Cheers,

    -stevieb

      I apologize: should I edit my post/question and add this information?
        ... should I edit my post/question and add this information?

        IMHO, not absolutely necessary after stevieb's post, but it would be a nice touch to do so. Please see How do I change/delete my post?.


        Give a man a fish:  <%-{-{-{-<

Re: Sub-setting an Array
by Laurent_R (Canon) on Sep 05, 2015 at 08:48 UTC
    Hi,

    a simple solution demonstrated under the Perl debugger:

    DB<1> @a = a..k; DB<2> x @a 0 'a' 1 'b' 2 'c' 3 'd' 4 'e' 5 'f' 6 'g' 7 'h' 8 'i' 9 'j' 10 'k' DB<3> for my $count (0..$#a) { $start = $count - 2; $start = 0 if $ +start <0; print "@a[$start..$count]\n";} a a b a b c b c d c d e d e f e f g f g h g h i h i j i j k