Hashes are the best data structures to use when trying to create arrays with unique values, because the keys are guaranteed to be unique (duplicate keys are overwritten as opposed to being appended to the stack). When you start using hashes to do lookups for uniqueness, you've learned a valuable skill. Here's some commented code for how to do this:

# your arrays my @a = qw(a c d); my @b = qw(s d v); my @c = qw(l d s); my @d = qw(i c f); # the hash we'll use to preserve uniqueness my %unique = (); # for every character in each of the defined arrays for my $char (@a, @b, @c, @d) { # whenever we run into a character, we set its # corresponding value to 1 in the unique hash. Even if we # run into the same character more than once, it'll only have # a single entry in our hash, as is the nature of hashes. $unique{$char} = 1; } # now, the keys in the %unique hash should be the unique # values from those arrays. We'll sort the keys for good # measure my @unique = sort keys %unique; # @unique is now the unique elements of those arrays. Kudos.

Hashes are your friend (I use them a LOT when looking for duplicates in company data files). Learn to use them wisely and you'll appreciate perl's power and simplicity a lot sooner.

Or, for a quickie:

my %unique = map { $_=>1 } @a, @b, @c, @d; my @unique = sort keys %unique;

--
perl: code of the samurai


In reply to Re: How to connect more arrays to a new array by samurai
in thread Find unique elements from multiple arrays by Anonymous Monk

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.