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

Hi all, I had to implement a co-relation problem in perl. I have two arrays i.e. array1 and array2 which contain following values.
array1 array2 read book eat apple book novel apple banana play football tennis football novel mazagine
I want my output as:
array1 array2 array3 array4 read book novel magazine eat apple banana play football tennis
i tried implementing it with simple for loops as:
for($i=0;$i<$arraylength;$i++) { for($j=0;$j<$arraylength;$j++) { if($array2[$i]== $array1[$j]) { $array3[$i] = $array2[$j]; } } } for($i=0;$i<$arraylength;$i++) { for($j=1;$j<$arraylength;$j++) { if($array2[$i]== $array2[$j]) { $array3[$i] = $array1[$j]; } } } for($i=0;$i<$arraylength;$i++) { for($j=0;$j<$arraylength;$j++) { if($array2[$i]== $array1[$j]) { $array4[$i] = $array3[$j]; } } }
But it was not showing the proper output. It was shows some random output in array3 and array4. Then a perl monk gave me a suggestion how to implemet and which works.The code is:
#!perl use strict; use warnings; my @array1 = qw(read eat book apple play football novel); my @array2 = qw(book apple novel banana football tennis magazine); # The follows relationship my %follow; @follow{@array1} = @array2; # Keeping track of what followers I have already used my %h2 = map {$_=>1} @array2; # Start with things that are not followers my @out = ([grep !exists $h2{$_}, @array1]); # Put followers of the previous column on as a new column while (%h2) { push @out, [@follow{@{$out[-1]}}]; delete @h2{@{$out[-1]}}; } use Data::Dumper; print Dumper(\@out); END { use Data::Dumper; print Dumper(\@out); }
But the problem is I have to insert each column value into each of mysql column.i.e. as follows:
read eat play in Column1 of the table in mysql book apple book in Column2 of the table in mysql novel banana tennis in Column3 of the table in mysql magazine in Column4 of the table in mysql
I tried implementing this by
#my $insert= $dbh->prepare("INSERT INTO Table values (?,?,?,?)"); my $san=0; my @array1 = qw(read eat book apple play football novel); my @array2 = qw(book apple novel banana football tennis magazine); # The follows relationship my %follow; @follow{@array1} = @array2; # Keeping track of what followers I have already used my %h2 = map {$_=>1} @array2; # Start with things that are not followers my @out = ([grep !exists $h2{$_}, @array1]); # Put followers of the previous column on as a new column while (%h2) { push @out, [@follow{@{$out[-1]}}]; delete @h2{@{$out[-1]}}; } use Data::Dumper; print Dumper(\@out); $insert->execute($out[0],$out[1],$out[2],$out[3]) END { use Data::Dumper; print Dumper(\@out); print "@out[1]"; } $insert->finish(); $dbh->disconnect or warn $dbh->errstr;
But it doesnt shoe the proper output. Infact some some value like
ARRAY(0x832d990) ARRAY(0x832d9xx) ARRAY(0x832d943)
Please guide me.. Thanking you

Replies are listed 'Best First'.
Re: insert perl dumper value in mysql
by bellaire (Hermit) on Mar 19, 2009 at 13:03 UTC
    On this line:
    my @out = ([grep !exists $h2{$_}, @array1]);
    The square brackets [] convert the array into an anonymous array reference (which is a single scalar value, it gets printed as ARRAY(0x832d990) for example).

    You probably want this:
    my @out = (grep !exists $h2{$_}, @array1);
      Thanks for ur reply.. I tried with ur suggestion... but now it shows only the 1st array.. I.e. it displays only..
      read', 'eat', 'play
      I cant find the other values. \n Also, how can i insert the entire values i.e read eat play in one column. Am able to insert only one value by using for eg one case in which "eat" is inserted into a column
      $insert->execute($out[1]);
        Oops, sorry, I misunderstood. I know that you're trying to insert multiple values, but you can't supply array references to that single insert statement and expect that it's going to insert multiple rows. It's possible to do with one statement, but that involves building a statement which has as many question marks as you have values to pass. It'd be easier to simply loop over the values in your @out array and insert them one at a time.

        For example, with the insert statment you have, something like:
        for my $idx (0..$#{$out->[0]}) { ...prepare your insert statmenet here, inside the loop... $insert->execute($out[0]->[$idx],$out[1]->[$idx],$out[2]->[$idx],$ +out[3]->[$idx]) }
        That'll run many insert statements, but should do what you want (I think!). Note that I'm not passing $out[0] itself, for example, but rather each element as indexed by $idx in turn.