array1 array2 read book eat apple book novel apple banana play football tennis football novel mazagine #### array1 array2 array3 array4 read book novel magazine eat apple banana play football tennis #### 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]; } } } #### #!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); } #### 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 #### #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; #### ARRAY(0x832d990) ARRAY(0x832d9xx) ARRAY(0x832d943)