On 30/11/2011 23:06, Jeswin wrote:
I've been trying to figure out how to print 2 arrays into 2 columns. I
came across this code and it works but gives me a problem in the
output.
*************************BEGIN CODE*********************************************
sub transpose {
map { my $i = $_; [ map $_->[ $i ], @_ ] } 0 .. $#{ $_[0] }
}
print "@$_\n" for transpose \( @unknown_abs, @unknown_conc );
*************************END
CODE************************************************
*************************BEGIN OUTPUT******************************************
0.1
0.729843646443355
0.2
1.47255203636502
0.35
2.58661462124751
0.33
2.43807294326318
0.41 3.03223965520051
*************************END OUTPUT*********************************************
So 0.1, 0.2, 0.35, 0.33, 0.41 should be in one column and the other
values next to it. I'm trying to figure out how to reduce precision
with printf but not really sure about that.
If I am understanding you correctly then there is no need for anything
so convoluted as your transpose subroutine. The program below does what
you need.
HTH,
Rob
use strict;
use warnings;
my @unknown_abs = ( 0.1, 0.2, 0.35, 0.33, 0.41 );
my @unknown_conc = ( 0.729843646443355, 1.47255203636502,
2.58661462124751, 2.43807294326318, 3.03223965520051 );
for my $i (0 .. $#unknown_abs) {
printf "%4.2f %4.2f\n", $unknown_abs[$i], $unknown_conc[$i];
}
**OUTPUT**
0.10 0.73
0.20 1.47
0.35 2.59
0.33 2.44
0.41 3.03
--
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/