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

supriyoch_2008 has asked for the wisdom of the Perl Monks concerning the following question:

Hi PerlMonks,

I am interested in finding the unique elements of each array from a set of input arrays (here only 3 arrays shown i.e. x,y & z). I have wriiten a script n.pl which can do this task easily and gives correct result.

When the number of input arrays will increase to 200 or more, I intend to use the other script m.pl (given below) which makes use of a LOOP. But m.pl gives wrong result. I think the incorrect result is due to scalar varible "s" which does not change to scalar s1, s2, s3 etc. while passing through the LOOP. That is why the hexadecimal values of the array 's' are exactly same. I am looking forward to perl monks for suggestions so that I can use m.pl to input many arrays for comparison.

Here goes the correct script n.pl

#!/usr/bin/perl use warnings; use List::Compare; # use of module @x= qw(b); @y= qw(c); @z= qw(d); $s1=\@x; push @s,$s1; $s2=\@y; push @s,$s2; $s3=\@z; push @s,$s3; print "\n Array s: @s\n"; $all= List::Compare->new(@s); # Function of module @unq_x=$all->get_unique(0); @unq_y=$all->get_unique(1); @unq_z=$all->get_unique(2); print "\n unq_x: @unq_x\n unq_y: @unq_y\n unq_z: @unq_z\n\n"; exit;

Correct results obtained from n.pl is given below:

C:\Users\x\Desktop>n.pl Array s: ARRAY(0x2140b14) ARRAY(0x2140ae4) ARRAY(0x215a024) unq_x: b unq_y: c unq_z: d

Here goes the script m.pl (incorrect). I am interested to use either for LOOP, foreach LOOP or any other LOOP here:

# To find unique elements in each array: #!/usr/bin/perl use warnings; use List::Compare; # Use of module @x= qw/b/; push @p,@x; @y= qw/c/; push @p,@y; @z= qw/d/; push @p,@z; $num=-1; for (@p) {$num++; # for LOOP starts @arr = $p{$num}; $s=\@arr; # Problem is here. # If 's' becomes s1, s2, s3 while pas +sing # through LOOP then the program may work. # How can I change s to s1, s2, s3 etc.? push @s,$ref; } # for LOOP ends $num_ele=@s; print "\n Array s: @s\n No. of Elements: $num_ele\n"; $all= List::Compare->new(@s); # module @unq_x=$all->get_unique(0); @unq_y=$all->get_unique(1); @unq_z=$all->get_unique(2); print " unq_x: @unq_x\n unq_y: @unq_y\n unq_z: @unq_z\n\n"; exit;

The incorrect results of m.pl are:

C:\Users\x\Desktop>m.pl Array s: ARRAY(0x2212004) ARRAY(0x2212004) ARRAY(0x2212004) No. of Elements: 3 unq_x: unq_y: unq_z:

Correct results of m.pl should look like:

Array s: ??? No. of Elements: 3 unq_x: b unq_y: c unq_z: d