[ABE.pm] Printing an array

Faber Fedor faber at linuxnj.com
Mon Apr 25 09:33:32 PDT 2005


I've created an array of arrays like this:

foreach $a (@{$ary_ref}) {

    $sum += $a->[1];
    # let our data struct be:
    # decile, cusip, weight, sum
    push(@deciles, (1, $a->[0], $a->[1], $sum)) if $sum <= 10.0;
    push(@deciles, (2, $a->[0], $a->[1], $sum)) if $sum <= 20.0 and $sum > 10;
    push(@deciles, (3, $a->[0], $a->[1], $sum)) if $sum <= 30.0and $sum > 20;
    push(@deciles, (4, $a->[0], $a->[1], $sum)) if $sum <= 40.0and $sum > 30;
    push(@deciles, (5, $a->[0], $a->[1], $sum)) if $sum <= 50.0and $sum > 40;
    push(@deciles, (6, $a->[0], $a->[1], $sum)) if $sum <= 60.0and $sum > 50;
    push(@deciles, (7, $a->[0], $a->[1], $sum)) if $sum <= 70.0and $sum > 60;
    push(@deciles, (8, $a->[0], $a->[1], $sum)) if $sum <= 80.0and $sum > 70;
    push(@deciles, (9, $a->[0], $a->[1], $sum)) if $sum <= 90.0and $sum > 80;
    push(@deciles, (10, $a->[0], $a->[1], $sum)) if $sum <= 100.0and $sum > 90;

}

(If anyone has a more elegant way of generating deciles or a better data
structure, let me know).

Now I want to print out @deciles one "element" (read: array) at a time
so that it looks like this:

    1 ABC 0.01 0.01
    1 DEF 0.02 0.03
    ...
    10 XXX 43.2 99.99
    10 ZZZ 45.0 100.00

but I can't figure out how.  When I say

    open (DECILES, ">deciles_$_->{index}.txt");
    for my $i (0..$#deciles){
        print DECILES "@{deciles[$i]}\n";
    }
    close(DECILES);

I get a \n after each element, e.g.

1
ABC
0.01
0.01
1
DEF
0.02
0.03
...

How can I print this data out in CSV format?  Or should I be using some
other data structure?

TIA


-- 
 
Regards,
 
Faber Fedor
President
Linux New Jersey, Inc.
908-320-0357
800-706-0701

http://www.linuxnj.com





More information about the ABE-pm mailing list