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

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!
  • Comment on Re^2: Is there an easy way to assign guaranteed unique values to a simple array without looping through whole array?
  • Select or Download Code

Replies are listed 'Best First'.
Re^3: Is there an easy way to assign guaranteed unique values to a simple array without looping through whole array?
by kyle (Abbot) on Nov 29, 2007 at 22:35 UTC

    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!