in reply to Until loop / unique

Sorry, I don't understand what you want...

Hashes are unsorted in Perl, there is nothing like "second keys".

Programming is an art of speaking, maybe it helps rephrasing your ideas in proper English?

-> How do I post a question effectively?

Cheers Rolf

( addicted to the Perl Programming Language)

Replies are listed 'Best First'.
Re^2: Until loop / unique
by monkini (Initiate) on Nov 11, 2013 at 22:14 UTC

    oops sorry, I was unclear here. The hash looks like:

    $hash{$key1}{$key2} = $value

    The subroutine 'retrieve_kv' already retrieves the other key and corresponding value, my question is how to put it in the loop, so that the keys retrieved are unique (there can be duplicates). Basically sth like:

    do { my ($key, $val) = retrieve_kv($hash{$k}); } until (ALL 5 KEYS ARE UNIQUE);
        Rolf, I think the OP means something like this:

        #!/usr/bin/perl -w use strict; use Data::Dumper; use debug; { my %tstinf = (); $tstinf{'Animal'}{'Mammal'}{'Domesticated'}{'Dogs'}{'Poodle'} = 1; $tstinf{'Animal'}{'Mammal'}{'Domesticated'}{'Dogs'}{'Greyhound'} = + 1; $tstinf{'Animal'}{'Mammal'}{'Domesticated'}{'Weasels'} = 1; $tstinf{'Animal'}{'Mammal'}{'Undomesticated'}{'Cats'}{'Siamese'} = + 1; $tstinf{'Animal'}{'Mammal'}{'Undomesticated'}{'Cats'}{'Calico'} = +1; $tstinf{'Animal'}{'Reptile'}{'Land'}{'Lizard'}{'Gekko'} = 1; $tstinf{'Animal'}{'Reptile'}{'Land'}{'Lizard'}{'Salamander'} = 1; $tstinf{'Animal'}{'Reptile'}{'Land'}{'Tortoise'}{'Spengleri'} = 1; $tstinf{'Animal'}{'Reptile'}{'Water'}{'Mocassin'}{'Blackhead'} = 1 +; $tstinf{'Animal'}{'Avian'}{'Bird'}{'Predator'}{'Raptor'} = 1; $tstinf{'Vegetable'}{'Ground Plant'}{'Edible'}{'Leafy'}{'Lettuce'} + = 1; my @unikey = &getUniqueKeys('', \%tstinf); print Dumper \@unikey; }

      Use List::MoreUtils::uniq().

      use strict; use warnings; use List::MoreUtils qw( uniq ); my %deck; for my $suit (qw( Clubs Hearts Diamonds Spades )) { for my $rank (qw( 2 3 4 5 6 7 8 9 10 J Q K A )) { $deck{$suit}{$rank} = 1; } } $, = " "; $\ = "\n"; # Prints 10 2 3 4 5 6 7 8 9 A J K Q print sort { $a cmp $b } uniq map { keys $deck{$_} } keys %deck;