Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??

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

In reply to How can one find unique elements from arrays using a LOOP? by supriyoch_2008

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.
  • Log In?
    Username:
    Password:

    What's my password?
    Create A New User
    Domain Nodelet?
    Chatterbox?
    and the web crawler heard nothing...

    How do I use this?Last hourOther CB clients
    Other Users?
    Others perusing the Monastery: (3)
    As of 2025-06-13 01:01 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      No recent polls found

      Notices?
      erzuuliAnonymous Monks are no longer allowed to use Super Search, due to an excessive use of this resource by robots.