in reply to Print multiple hashes of arrays at same time
#!/usr/bin/perl use strict; use warnings; my $best = {1 =>[1,2]}; my $kept_best = {1 =>[1]}; my $width = 10; my $precision = 5; printf "%-${width}s %-${width}s\n", "BEST", "SELECTED BEST"; foreach my $value (sort keys %$best){ foreach my $bind_energy (@{$best->{$value}}){ my $kept = shift @{$kept_best->{$value}}; my $format = "\%${width}.${precision}f"; $format .= defined $kept ? " $format\n" : "\n"; printf $format, $bind_energy, $kept; } }
If you find the variable $format approach unclear, you can achieve the same result by just using multiple printfs and an if conditional:
#!/usr/bin/perl use strict; use warnings; my $best = {1 =>[1,2]}; my $kept_best = {1 =>[1]}; my $width = 10; my $precision = 5; my $format = "\%${width}.${precision}f "; printf "%-${width}s %-${width}s\n", "BEST", "SELECTED BEST"; foreach my $value (sort keys %$best){ foreach my $bind_energy (@{$best->{$value}}){ printf $format, $bind_energy; my $kept = shift @{$kept_best->{$value}}; printf $format, $kept if defined $kept; print "\n"; } }
TIMTOWTDI.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Print multiple hashes of arrays at same time
by tomdbs98 (Beadle) on Jul 19, 2010 at 19:04 UTC |