Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

Read Every n elements from Array

by vetrivel (Sexton)
on Feb 17, 2011 at 12:13 UTC ( #888698=perlquestion: print w/ replies, xml ) Need Help??
vetrivel has asked for the wisdom of the Perl Monks concerning the following question:

Hai ,
How to read every 3 elements from array . I have one way for doing this . Could you tell me any other best way to achieve this .
use Data::Dumper; $test = [ "1" , "2" ,"3" , "4" ,"5" ,"6" ,"7" , "8" ,"9" ,"10" ]; while( @$test ) { @new_Array = splice( @$test , 0 , 3 ) ; print Dumper( \@new_Array ) ; }

Comment on Read Every n elements from Array
Download Code
Re: Read Every n elements from Array
by davido (Archbishop) on Feb 17, 2011 at 12:24 UTC

    I'm interpreting the language of your question to mean read every third element, starting with the first one. That's one situation where a good old "c-style loop" comes in handy:

    use strict; use warnings; use feature qw/say/; my @array = qw/0 1 2 3 4 5 6 7 8 9 10 11 12/; for ( my $idx = 0; $idx < @array; $idx += 3 ) { say $array[$idx]; }

    perlsyn discusses the various loop types. Your splice method is destructive to the original array, and requires that a new array be set up to accommodate the elements you've selected. The method I demonstrated leaves the original array untouched. If you actually want a new array, you could set one up and push the elements into it within the loop.

    Update: You may be asking to read three elements at a time, if I read the language of your question a different way. It's possible to read three elements at a time by just using offsets within the loop, and this would still be non-destructive to your original array.


    Dave

Re: Read Every n elements from Array
by Eliya (Vicar) on Feb 17, 2011 at 12:28 UTC

    There's nothing wrong with using splice (unless you want the operation to be non-destructive, which you haven't specified).

    Another solution would be List::MoreUtils's natatime.  The example in the docs does more or less exactly the same that your code is doing.

Re: Read Every n elements from Array
by LanX (Canon) on Feb 17, 2011 at 13:23 UTC
    well splice is destructive, you could alternatively use array slices:

    print @arr[$idx..$idx+2]; $idx+=3;

    or

    print @arr[$idx++,$idx++,$idx++];

    Cheers Rolf

      In general, reading and writing to a variable in the same statement can lead to problems. It's not a problem here, but that's not obvious at a glance. For example, that code has undefined behaviour in C. My compiler happens to get it "wrong":

      $ cat a.c #include <stdio.h> int main() { int i = 0; printf("%d %d %d\n", i++, i++, i++); return 0; } $ gcc -Wall -o a a.c && a a.c: In function ‘main’: a.c:5: warning: operation on ‘i’ may be undefined a.c:5: warning: operation on ‘i’ may be undefined 2 1 0

      It's simpler just to avoid the issue.

Reaped: Re: Read Every n elements from Array
by NodeReaper (Curate) on Feb 17, 2011 at 19:09 UTC
Re: Read Every n elements from Array
by ikegami (Pope) on Feb 17, 2011 at 19:16 UTC

    What kind of improvement are you looking for? That looks like a perfectly fine solution. How could it be any simpler?

    while (@a) { my @group = splice(@a, 0, 3); ... }
    for my $i (0..@a/3-1) { my @group = @a[$i*3..$i*3+2]; ... }
    for (my $i=0; $i<@a; $i+=3) { my @group = @a[$i..$i+2]; ... }
    use List::MoreUtils qw( natatime ); my $i = natatime(3, @a); while (my @group = $it->()) { ... }
Re: Read Every n elements from Array
by hbm (Hermit) on Feb 17, 2011 at 22:00 UTC

    Perhaps shortest?

    $test = [ "1" , "2" ,"3" , "4" ,"5" ,"6" ,"7" , "8" ,"9" ,"10" ]; @new_Array=grep++$i%3<1,@$test;

    Oops!

      That doesn't do the same thing at all. The OP grabs three at a time. That grabs every third.

        What is he trying to do with @new_Array as he is creating new array everytime.

        OP, Do you have any idea what are you trying to achieve here.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (7)
As of 2014-10-01 09:25 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    How do you remember the number of days in each month?











    Results (392 votes), past polls