Hey Monks
I'm trying to print and then remove duplicates from two arrays. By this I mean if the array 1 contains the number 3 and the array 2 contains the number 3 also. Then this would be printed and the numbers 3 removed from each array.
#!/usr/perl
use strict;
my @array1 = (0,1,2,3,4,5,6,7,8,9);
my @array2 = (0,1,4,);
my $num = "3";
my $array1size = @array1;
my $array2size = @array2;
for (my $count1=0;$count1 < $array1size;$count1++){
for (my $count2=0;$count2 < $array2size;$count2++){
if ($array1[$count1] == $array2[$count2]){
print $array1[$count1] . " is present in array1 and it (" . $a
+rray2[$count2] . ") is also present in array2!\n";
splice (@array1, $count1, 1);
splice (@array2, $count2, 1);
}
}
}
Any idea why this isn't working. My expected output is
O is present in array1 and it(0) is also present in array2!
1 is present in array1 and it(1) is also present in array2!
4 is present in array1 and it(4) is also present in array2!
Thanks
j
o
h
n
i
r
l
.
Sum day soon I'Il lern how 2 spelI (nad tYpe)
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: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.