in reply to compare and output to the screen
[ We'll both have an easier time if you use <c>...</c> around your code. ]
The key to repeating the same action for multiple inputs is to store the inputs in a hash or array, not 12 variables.
use strict; use warnings; sub compare_arrays { my ($array1, $array2) = @_; return 0 if @$array1 != @$array2; for my $i (0..$#$array1) { return 0 if $array1->[$i] ne $array2->[$i]; } return 1; } my %lists = ( D => [ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 ], E => [qq( a b c d e f g h i j k l m n o p q r s t u v w x y z )], F => [ 1, 3, 5, 7, 9 ], G => [qq( a b c d e f g h i j k l m )], H => [ 2, 4, 6, 8 ], K => [qq( n o p q r s t u v w x y z )], M => [ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 ], P => [qq( a b c d e f g h i j k l m n o p q r s t u v w x y z )], S => [ 1, 3, 5, 7, 9 ], T => [qq( a b c d e f g h i j k l m )], X => [ 2, 4, 6, 8 ], Z => [qq( n o p q r s t u v w x y z )], ); my @keys = keys %lists; for my $i1 (0..$#keys) { my $key1 = $keys[$i1]; for my $i2 ($i1+1..$#keys) { my $key2 = $keys[$i2]; if ( compare_arrays($lists{$key1}, $lists{$key2}) ) { print("$key1 is identical to $key2\n"); } else { #print("$key1 is different than $key2\n"); } } }
S is identical to F T is identical to G X is identical to H P is identical to E K is identical to Z M is identical to D
The reason for using
my @keys = keys %lists; for my $i1 (0..$#keys) { my $key1 = $keys[$i1]; for my $i2 ($i1+1..$#keys) { my $key2 = $keys[$i2];
instead of
for my $key1 (keys %lists) { for my $key2 (keys %lists) {
is to avoid useless duplication of outputs.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: compare and output to the screen
by coronalvrr (Initiate) on Apr 25, 2009 at 16:20 UTC |