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

Hi I would like to loop through multiple arrays size of the array might not be same. I need to insert the data from the array to database
@arrray1 = ['a','b','c']; @array2 = ['a','b']; @array3 = ['a','b','c','d']; while(my($array1,$array2,$array3) = $iteration->()){ warn " iteration : $array1,$array2,$array3"; $sthgetCompany->execute($aray2) or die 'Cannot execute the statem +ent'. $dbh_oracle->errstr;; #insertion to the database goes here. } # end of while loop #.
I tried using ListMore::Utils each_array but its trying to insert for 9 times with null statements where it can't find pairs from each array. How can I handle multiple arrays with different size.

Thanks

Replies are listed 'Best First'.
Re: Looping through multiple arrays
by choroba (Cardinal) on Mar 05, 2015 at 11:51 UTC
    The first step: Fix the input. In your code sample, you created three arrays, each of them with just one member: an array reference.

    Square brackets introduce array references. Use round parentheses () for lists.

    @array1 = ('a','b','c'); @array2 = ('a','b'); @array3 = ('a','b','c','d');

    How do you want to group the results?

    #! /usr/bin/perl use warnings; use strict; use Syntax::Construct qw{ // }; my @array1 = ('a','b','c'); my @array2 = ('a','b'); my @array3 = ('a','b','c','d'); while (@array1, @array2, @array3) { print join ', ', map shift @$_ // 'NULL', \@array1, \@array2, \@ar +ray3; print "\n"; }
    لսႽ† ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ
      Sorry, its an array and not an array reference. Do you think this is legal?
      next if($array1|$array2|$array3) eq '';
      Seems to have solved the issue so far.
Re: Looping through multiple arrays
by Discipulus (Canon) on Mar 05, 2015 at 11:51 UTC
    You are wrong in declaring the array:
    perl -MData::Dumper -e "@arr=['a','b']; print Dumper (\@arr)" $VAR1 = [ [ 'a', 'b' ] ];
    You are declaring arrays with only one element which turns to be an anonyomous array.

    Without getting deep in what your data means or meant to be used, you have to do something simple as:
    perl -MData::Dumper -e "@arr1=qw(a b);@arr2=qw(c d);foreach (@arr1,@ar +r2){print qq([$_]\n) }" [a] [b] [c] [d]
    HtH
    L*
    There are no rules, there are no thumbs..
    Reinvent the wheel, then learn The Wheel; may be one day you reinvent one of THE WHEELS.
Re: Looping through multiple arrays
by karlgoethebier (Abbot) on Mar 05, 2015 at 17:23 UTC

    Yet another addendum:

    If you really want to use an iterator using List::MoreUtils, perhaps because you think that it might perform better then using for - according to the pure doctrine, you could try something like this:

    #!/usr/bin/env perl use strict; use warnings; use List::MoreUtils qw (natatime); use feature qw (say); use Data::Dump; my @x = qw( a b c ); my @y = qw( a b ); my @z = qw( a b c d ); dd \@x, \@y, \@z; my $iterator = natatime 1, ( @x, @y, @z ); while ( my $value = $iterator->() ) { say $value; } __END__ karls-mac-mini:monks karl$ ./1118875.pl (["a", "b", "c"], ["a", "b"], ["a" .. "d"]) a b c a b a b c d

    Best regards, Karl

    «The Crux of the Biscuit is the Apostrophe»

      Nothing more than a point on style ... interesting placement of () (in that nata-time function); I write that as ...

      my $iter = natatime( $chunk_size , @x , @y ... );
        "... a point on style...interesting placement of ()"

        Perlish ;-)

        "Variatio delectat" ([transl. Diversity delights] Cicero)

        «The Crux of the Biscuit is the Apostrophe»

A reply falls below the community's threshold of quality. You may see it by logging in.