in reply to Algorithm needed

I tried the following revised code, but it neglects to push unique values onto each individual array.
#!/usr/bin/perl -w use strict; my %seen; my %hash = (); while (<DATA>) { # split based on spaces my ($hex, $foo, $int) = split /\s+/; # use hash value as an array ref push @{$hash{$hex}}, $int unless $seen{$int}++; } for (keys %hash) { print "$_ = ", join(", ", sort @{$hash{$_}}), "\n"; } __DATA__ 0x130005d 1.253.54.1 11 0x130009c 1.253.54.2 12 0x130005d 1.253.54.1 14 0x130005d 1.253.54.1 11 0x130005d 1.253.54.1 10 0x130009c 1.253.54.2 12 0x130009c 1.253.54.2 14

Replies are listed 'Best First'.
Re: Re: Algorithm needed
by LTjake (Prior) on Dec 06, 2002 at 00:57 UTC
    Using merlyn's code (see this node):
    use strict; my %hash; while (<DATA>) { # split based on spaces my ($hex, $foo, $int) = split /\s+/; $hash{$hex}{$int}++; } for (keys %hash) { print "$_ = ", join(", ", sort keys %{$hash{$_}}), "\n"; } __DATA__ 0x130005d 1.253.54.1 11 0x130009c 1.253.54.2 12 0x130005d 1.253.54.1 14 0x130005d 1.253.54.1 11 0x130005d 1.253.54.1 10 0x130009c 1.253.54.2 12 0x130009c 1.253.54.2 14
    Gives
    0x130005d = 10, 11, 14 0x130009c = 12, 14


    --
    "I don't intend for this to take on a political tone. I'm just here for the drugs." --Nancy Reagan