in reply to Is there an easy way to assign guaranteed unique values to a simple array without looping through whole array?

You can create an ordered hash with Tie::IxHash. Or you can filter duplicates using List::MoreUtils's uniq function. (Which basically just uses a hash to do it anyway.) But I think perhaps you're making the problem more complicated than it needs to be. If you describe in more detail what it is you're trying to accomplish, we might be able to offer better solutions.
  • Comment on Re: Is there an easy way to assign guaranteed unique values to a simple array without looping through whole array?
  • Download Code

Replies are listed 'Best First'.
Re^2: Is there an easy way to assign guaranteed unique values to a simple array without looping through whole array?
by memnoch (Scribe) on Nov 29, 2007 at 22:16 UTC
    Thank you friedo! Actually, I just realized that I could do something like:
    my $test; unless (exists($hash{$test})) { $hash{$test} = localtime; # For sorting later on }
    Then I can do:

    sort keys(%hash)

    to get the keys in the inserted order.....I'll have to test it later.

    memnoch

    Gloria in Excelsis Deo!

      I'd suggest using a counter variable rather than localtime (did you mean time?).

      my $count = 0; my $test; unless ( exists $hash{$test} ) { $hash{$test} = $count++; }

      This way, you can be sure you don't have duplicate values in the hash, even if you insert two elements at the same second.

      To do the sorting, you'd need to do this:

      sort { $hash{$a} <=> $hash{$b} } keys %hash;
        Thanks Kyle! I haven't used the spaceship operator yet....I'll have to look into that!

        memnoch
        Gloria in Excelsis Deo!