in reply to Storing unique values

This should do what you're looking for:
#!/perl/bin/perl use warnings; use strict; my @arr = qw(AB1 BX6 WF2 CB7 EZ6 WY2 AB1 AB1 WF2 ET9); my $hash = {}; for my $cur (@arr) { if (exists($hash->{$cur})) { $hash->{$cur}++; } else { $hash->{$cur} = 1; } } my @unique = sort keys %$hash; for (@unique) { print sprintf qq(Value "%s" occurred %d times.\n), $_, $hash->{$_}; }

As you parse through the source array, you can just check for existence in the %$hash, and then process/ignore as necessary.

Where do you want *them* to go today?

Replies are listed 'Best First'.
Re^2: Storing unique values
by Aristotle (Chancellor) on Jan 18, 2003 at 20:28 UTC
    if (exists($hash->{$cur})) { $hash->{$cur}++; } else { $hash->{$cur} = 1; }
    No need to jump through hoops. Perl doesn't complain when increasing undefined variables, so this will do:
    $hash->{$cur}++;
    It's the accept practice for this case, in fact, so it should be done this way as well.

    Makeshifts last the longest.