Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
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":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
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 having a coffee break in the Monastery: (5)
As of 2024-05-30 06:38 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found