Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight

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"; }
a	1	c	3	b	2
a	11	b	12
a	1	c	21

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://243312]
[choroba]: Perl to the rescue!
[Corion]: hippo: At least for the German translation, the wordplay is translated as well
[Discipulus]: always! but if i can ask choroba why you always use the inverted (to me) form 1 == $.?
[choroba]: To avoid accidental overwriting of the variable in if ($x = 3)
[karlgoethebier]: Corion: Auch in hessisch? Mer wasses net.
[Corion]: Hmm - I'm not sure, I haven't read the mundart/slang translations ;)
[Eily]: $F[1] =~ s/^0//r could be written as (0+$F[1]). It's less explicit though

How do I use this? | Other CB clients
Other Users?
Others examining the Monastery: (14)
As of 2017-05-24 08:40 GMT
Find Nodes?
    Voting Booth?