On 11/30/11 Wed  Nov 30, 2011  3:06 PM, "Jeswin" <phillyj...@gmail.com>
scribbled:

> 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.

Capture the output of transpose in an array variable:

  my @array = transpose \( @unknown_abs, @unknown_conc  );

and print in a loop:

  for my $ent ( @array ) {
    printf("  %3.2f:  %8.4f\n", @$ent);
  }

Output:

  0.10:    0.7298
  0.20:    1.4726
  0.35:    2.5866
  0.33:    2.4381
  0.41:    3.0322

(but give your array variable a more decsriptive name!)




-- 
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/


Reply via email to