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

Replies are listed 'Best First'.
Re: How can one find unique elements from arrays using a LOOP?
by choroba (Cardinal) on Mar 04, 2014 at 10:58 UTC
    You are assigning a single element to an array, namely a value from a (non-existent) hash:
    @arr = $p{$num};

    Using strict would have told you.

    Also,

    push @p, @x; push @p, @y;

    is effectively the same as

    push @p, $_ for @x, @y;

    Why are you pushing array references in the first example, but arrays in the second one?

    لսႽ ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ
        I had the feeling the OP might still understand that as
        push @p, \@x, \@y
        لսႽ ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ

      Hi choroba,

      Thank you for your suggestions and prompt reply. My problem appears to be solved. Your last sentence (question) has been very constructive and ignited my thinking. It has helped me to sort out the problem. I have corrected the script m.pl and it goes as follows. I am grateful to you.

      With regards,

      Here goes the code m.pl:

      #!/usr/bin/perl use warnings; use List::Compare; # Use of module @x= qw/a b/; @y= qw/a c/; @z= qw/a d/; push @s,\@x,\@y,\@z; $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 results of m.pl are (correct):

      C:\Users\x\Desktop>m.pl Array s: ARRAY(0x320b14) ARRAY(0x320b64) ARRAY(0x3305d4) No. of Elements: 3 unq_x: b unq_y: c unq_z: d