http://www.perlmonks.org?node_id=681300


in reply to Re: Array name with a Variable
in thread Array name with a Variable

I do actually want to keep the intersection of the array to itself. I also tried this in my script and ran into an issue. I then tried it against the basic example to see if I can follow the steps. The output is as follows:
c d e 3 c d e 3 c d e 3 c d e 3 e 1 e f g h 4 e f g h 4 e f g h 4 d 1 ....
I thought it would be more like this:
cde 3 e 1 d 1 0 e 1 ....
I thought it would compare @array1 to @arrays1 - 4 then compare @array2 to @arrays1 - 4 and so forth. I think I am just not following the loops. Thanks-

Replies are listed 'Best First'.
Re^3: Array name with a Variable
by mr_mischief (Monsignor) on Apr 17, 2008 at 21:30 UTC
    Actually, I think I have a bug. Let me look a little closer. Yes, I have a bug. I moved the declaration of the variables into the wrong scope. This should work a little better.

    use strict; use warnings; my @array1 = qw( c d e ); my @array2 = qw( e f g h ); my @array3 = qw( a b d ); my @array4 = qw( s g h j k l ); open my $out, '>', 'output' or die "Cannot open: $!\n"; foreach my $a1 ( \@array1, \@array2, \@array3, \@array4 ) { foreach my $a2 ( \@array1, \@array2, \@array3, \@array4 ) { my ( %union, %intersect ); foreach my $item1 ( @$a1 ) { foreach my $item2 ( @$a2 ) { # print "item1: $item1\nitem2: $item2\n"; $union{$item1}++; $union{$item2}++; if ( $item1 eq $item2 ) { $intersect{$item1}++; } } } my @intersect = sort keys %intersect; print $out "@intersect\n"; #prints intersecting words print $out scalar @intersect . "\n"; } } close $out or die "Error closing output; $!\n";

      [estrawser@bast1 test]$ cat arr #!/usr/bin/perl for($i=0;$i<5;$i++){ push (@$i, "foo bar $i"); print "@$i "; } print "\n"; for($x=0;$x<5;$x++){ print "@$x "; } print "\n"; [estrawser@bast1 test]$ ./arr foo bar 0 foo bar 1 foo bar 2 foo bar 3 foo bar 4 foo bar 0 foo bar 1 foo bar 2 foo bar 3 foo bar 4