I am going to assume the code was meant to look like so until you fix your code tags.

my @arr_in_1 =("User1","user2","user3"); my @arr_in_2 =("user2","user3","User4"); my @union_arr; foreach my $arr_1(@arr_in_1) { push(@union_arr,$arr_1); } foreach my $arr_1(@arr_in_1) { #print "The value in array 1 is :" .lc $arr_1."\n"; foreach my $arr_2(@arr_in_2) { #print "the value in array 2 is :". lc $arr_2." \n"; if (lc $arr_2 ne lc $arr_1) { push(@union_arr,$arr_2); } } } print "union array @union_arr";
The main problem is the statement lc $arr_2 ne lc $arr_1 which will always result in truth (in this case). For instance think about what is being compared.

first loop(first element in @arr_in_1) vs inner loop(@arr_in_2):
user1 ne user2 (true)
user1 ne user3 (true)
user1 ne user4 (true)
so obviously this is inserted into the union array.

second loop:
user2 ne user2 (false)
user2 ne user3 (true) Again pushed into the union array
...

Well you get my drift. Your on the right track just have to make a couple modifications.

-enlil


In reply to Re: Union of arrays by Enlil
in thread Union of arrays by Ananda

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.