Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

Re: Printing a hash in a specific order?

by artist (Parson)
on Mar 15, 2003 at 16:23 UTC ( #243312=note: print w/ replies, xml ) Need Help??


in reply to Printing a hash in a specific order?

Another approach

my @records; $records[1] = { a => 1, d=>4, b=> 2, c=> 3}; $records[2] = { a => 11, d=>14, b=>12}; # didn't define 'c' here $records[3] = { a => 123, d=>10, e=>200, c=> 21, d=>9888 }; # differen +t order, 'b' absent my @keys = qw( a c b); # only want to print a c and b foreach my $record (@records){ print join "\t" => map { $_, length($record->{$_}) < 3 ? $record->{$_} : substr($record->{$_},0,1)} grep { defined $record->{$_}} @keys, "\n"; print "\n"; }
Output:
a	1	c	3	b	2
a	11	b	12
a	1	c	21
artist


Comment on Re: Printing a hash in a specific order?
Download Code

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others meditating upon the Monastery: (5)
As of 2015-07-05 13:55 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (67 votes), past polls