in reply to Matching Values in Arrays

You want a hash or an array to store references to your three arrays. http://perl.plover.com/varvarname.html has a better explanation of how to approach your problem than I can ever write.

The short of it is that you want:

my @arrays = (\@array1, \@array2, \@array3); for my $row (0..$#${ $arrays[0] }) { print "$row: "; for my $array (@arrays) { print $array->[$row], ";"; }; print "\n"; };

Also see tye's References Quick Reference for more information about references

Replies are listed 'Best First'.
Re^2: Matching Values in Arrays
by mikejones (Scribe) on Aug 07, 2007 at 17:52 UTC
    Your code is not correct. You are trying to print a SCALAR ref that does not exist. Here is what I have:
    use strict; use warnings; local $, = "\t"; my @array1= qw(Bob smith); my @array2= qw(Jen smith); print (@array1, @array2); print "\n"; my @arrays = (\@array1, \@array2); for my $row (0..$#{ $arrays[0] }) { print "$row: "; for my $array (@arrays) { print $array->[$row], ";"; }; print "\n"; }; __OUTPUT__ Bob smith Jen smith 0: Bob ;Jen ; 1: smith ;smith ;
      Another version:
      use strict; use warnings; local $, = "\t"; my @array1= qw(Ben bobby smith); my @array2= qw(Ben laurie smith); print (@array1, @array2); print "\n"; my @arrays = (\@array1, \@array2); use Data::Dumper; print Dumper ($arrays[0]),"\n"; for my $row (0..$#{ $arrays[0] }) { print "ROW:$row: "; for my $array (@arrays) { print $array->[$row], ";"; if ($array1[$row] eq $array2[$row]) { print $array->[$row], ";"; print qq{ \nElements $row are equal with values of \n$array1[$row] and $array2[$row] in \n$@arrays\n }; last; } }; print "\n"; };