in reply to Re: Until loop / unique
in thread Until loop / unique

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);

Replies are listed 'Best First'.
Re^3: Until loop / unique
by LanX (Saint) on Nov 11, 2013 at 22:17 UTC
      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; }
Re^3: Until loop / unique
by Jim (Curate) on Nov 11, 2013 at 23:27 UTC

    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;