in reply to a use for hashes?

my %relays; while (<>) { my ($id, $state) = split ' '; push @{ $relays{$id} }, $state; } for my $id (keys(%relays)) { print("$id @{ $relays{$id} }\n"); }

Replies are listed 'Best First'.
Re^2: a use for hashes?
by Spooky (Beadle) on Feb 02, 2010 at 14:22 UTC
    Thanks - that worked great! ..but it got me thinking. How could I do the same thing only instead of processing your code on a finished file if I could do the same thing as the file is being built? In other words, when code hits a relay that has already appeared with a state, the newly read state would be "pushed" along side of the previous one.
      Just replace the line that writes to the file with push @{ $relays{$id} }, $state;
        Thanks ikegami! ..one last ?: if I have a relay followed by a number of states, e.g., relay1 on off set reset, how would I create an array or hash where the first element would contain the relay and the second would contain all of the various states - i.e., $ele[0] = relay1 $ele1 = on off set reset?