Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation

Re: Algorithm needed

by elusion (Curate)
on Dec 05, 2002 at 23:14 UTC ( #217932=note: print w/replies, xml ) Need Help??

in reply to Algorithm needed

How's this do ya?
#!/usr/bin/perl -w use strict; 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; }
You can then access the ints by using the hash values as array refs. For instance, to print the output:
for (keys %hash) { print "$_ = ", join(", ", sort @{$hash{$_}}), "\n"; }

elusion :

Update: D'oh! Missed the part about ints being unique. merlyn saw it though.

Replies are listed 'Best First'.
•Re: Re: Algorithm needed
by merlyn (Sage) on Dec 05, 2002 at 23:26 UTC
    Or, just do it all at once:
    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"; }

    -- Randal L. Schwartz, Perl hacker
    Be sure to read my standard disclaimer if this is a reply.

      split /\s+/;

      Do you recommend this as normal practice? I think the special case, split ' ', is usually what people really want. (Yes, this is a very minor point but I'm curious.)

      "My two cents aren't worth a dime.";
Re: Re: Algorithm needed
by The_Rev (Acolyte) on Dec 05, 2002 at 23:26 UTC
    It works fine, except I only need the unique ints, because my real data file is quite large.


    0x130005d = 10, 11, 14 0x130009c = 12, 14
    This is the same problem I was having earlier.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://217932]
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others surveying the Monastery: (7)
As of 2019-07-22 09:49 GMT
Find Nodes?
    Voting Booth?
    If you were the first to set foot on the Moon, what would be your epigram?

    Results (12 votes). Check out past polls.