Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

Re: Sub-setting an Array

by 1nickt (Canon)
on Sep 05, 2015 at 00:02 UTC ( [id://1141083]=note: print w/replies, xml ) Need Help??


in reply to Sub-setting an Array

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.

Replies are listed 'Best First'.
Re^2: Sub-setting an Array
by Anonymous Monk on Sep 05, 2015 at 00:25 UTC
    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.
        That's not what I'm trying to do. My subset can over lap, so if the initial array is (a,b,c,d,e,f,g,h) the for loop should return:

        a

        a,b

        a,b,c

        b,c,d

        c,d,e

        d,e,f

        e,f,g

        f,g,h

        That's not what I'm trying to do. My subset can over lap, so if the initial array is (a,b,c,d,e,f,g,h) the for loop should return: a a,b a,b,c b,c,d c,d,e d,e,f e,f,g f,g,h

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1141083]
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: (7)
As of 2024-04-23 18:39 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found