in reply to Unique keys for multiple values with a Hash

My approach, to ensure uniqueness in values, would be to make the values the keys of hash and then transform them into an array reference.

#!/usr/bin/perl -w use strict; use Data::Dumper; my %hash; while (<DATA>) { next unless /[^[:space:]]/; # make sure there's something on the l +ine my ($key,$value) = split; $hash{$key}{$value} = 0; } foreach my $key (keys %hash) { $hash{$key} = [keys %{$hash{$key}}]; } print Dumper \%hash; __DATA__ 5 25 6 27 5 24 5 23 6 29 6 30 4 22 5 25 6 27 4 22 4 21

Update: Everyone seems to want to push the value on an array which doesn't get the "unique values" in the original request.

Cheers,
Ovid

New address of my CGI Course.
Silence is Evil (feel free to copy and distribute widely - note copyright text)

Replies are listed 'Best First'.
Re: Re: Unique keys for multiple values with a Hash
by Anonymous Monk on Jan 14, 2003 at 19:37 UTC
    Thanks, Ovid.

    That's exactly what I was looking for!
    Thanks again