[ 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.


In reply to Re: compare and output to the screen by ikegami
in thread compare and output to the screen by coronalvrr

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.