Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
PerlMonks  

Re^2: Sub-setting an Array

by Anonymous Monk
on Sep 05, 2015 at 00:25 UTC ( [id://1141084]=note: print w/replies, xml ) Need Help??


in reply to Re: Sub-setting an Array
in thread Sub-setting an Array

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?

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

    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

        Another approach, also using List::Util::max(), but using an array slice rather than splice:

        c:\@Work\Perl>perl -wMstrict -le "use List::Util qw(max); ;; my @ra = qw(a b c d e f g h); ;; my $n = 3; for my $i (0 .. $#ra) { my @triplet = @ra[ max(0, $i - $n + 1) .. $i ]; printf q{'%s' }, join '', @triplet; } " 'a' 'ab' 'abc' 'bcd' 'cde' 'def' 'efg' 'fgh'
        (The call to  max() could be replaced with a  ?: ternary if necessary.)


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

        Like this?

        #!/usr/bin/perl use strict; use warnings; my @array = qw/ c h e a p s l i d i n g w i n d o w /; my $elements = @array; for ( my $offset = 0; $offset < $elements; $offset++ ) { for my $length ( 1 .. 3 ) { next if $length != 3 and $offset >= 1; my @to_splice = @array; my @spliced = splice @to_splice, $offset, $length; print "@spliced\n"; } } __END__
        Output:
        $ perl 1141079.pl c c h c h e h e a e a p a p s p s l s l i l i d i d i d i n i n g n g w g w i w i n i n d n d o d o w o w w $

        Note that a "sliding window" is a Thing and if you do some research you'll probably find existing tools for implementing it in your program.

        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

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others lurking in the Monastery: (4)
As of 2024-04-23 06:44 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found