If you need to retain the same order in your unique array as the original a different approach is required as hashes are inherently unordered. The grep uses the %seen hash to only pass through data elements that it hasn't already seen, thus removing duplicates from the stream. The first (lower) map takes each line read from the input file and removes any trailing white space, including line terminators (I do this instead of chomping in case there are differing numbers of trailing spaces in the data). The grep removes duplicates as already described and the second (upper) map creates an anonymous array of data split on commas.

$ perl -Mstrict -Mwarnings -MData::Dumper -E ' open my $inFH, q{<}, \ <<EOF or die $!; server1,user1,% server1,user1,db1 server1,user2,% server1,user3,% server1,user1,% server1,user2,% server1,user2,db2 server1,user3,db3 server1,user3,% server2,user1,% server2,user1,db1 server2,user2,% server2,user3,% server2,user1,% server2,user2,% server2,user2,db2 server2,user3,db3 server2,user3,% server3,user1,% server3,user1,db1 server3,user2,% server3,user3,% server3,user1,% server3,user2,% server3,user2,db2 server3,user3,db3 server3,user3,% EOF my @logins = do { my %seen; map { [ split m{,} ] } grep { ! $seen{ $_ } ++ } map { s{\s*$}{}; $_ } <$inFH>; }; print Data::Dumper->Dumpxs( [ \ @logins ], [ qw{ *logins } ] );' @logins = ( [ 'server1', 'user1', '%' ], [ 'server1', 'user1', 'db1' ], [ 'server1', 'user2', '%' ], [ 'server1', 'user3', '%' ], [ 'server1', 'user2', 'db2' ], [ 'server1', 'user3', 'db3' ], [ 'server2', 'user1', '%' ], [ 'server2', 'user1', 'db1' ], [ 'server2', 'user2', '%' ], [ 'server2', 'user3', '%' ], [ 'server2', 'user2', 'db2' ], [ 'server2', 'user3', 'db3' ], [ 'server3', 'user1', '%' ], [ 'server3', 'user1', 'db1' ], [ 'server3', 'user2', '%' ], [ 'server3', 'user3', '%' ], [ 'server3', 'user2', 'db2' ], [ 'server3', 'user3', 'db3' ] ); $

I hope this is of interest.

Cheers,

JohnGG


In reply to Re: getting unique AOH from nonunique AOH ... or hash if it is better approach by johngg
in thread getting unique AOH from nonunique AOH ... or hash if it is better approach by hiyall

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.