Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
PerlMonks  

Re: Making a hash of arrays from a datafile

by TedPride (Priest)
on Oct 19, 2004 at 23:14 UTC ( #400696=note: print w/replies, xml ) Need Help??


in reply to Making a hash of arrays from a datafile

use strict; use warnings; my %hash; m/(\d+),(\d+)/ and push(@{$hash{$1}}, $2) for <DATA>; for (sort keys %hash) { print "$_ => "; print "$_ " for sort @{$hash{$_}}; print "\n"; } __DATA__ 2,6 2,3 1,2 2,5 1,3 1,4
This creates a hash with the first value as key and all the second values pushed into nested arrays. Sorting and printing is simple, as seen above.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others wandering the Monastery: (4)
As of 2021-05-09 22:53 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Perl 7 will be out ...





    Results (102 votes). Check out past polls.

    Notices?