brentheigold has asked for the wisdom of the Perl Monks concerning the following question:

I have an array, say i'm stepping through a debugger and the first two elements look like:
1 ARRAY(0x4779c0) 0 'IBM' 1 'B' 2 226110 3 22020702 4 545 5 12.5109 2 ARRAY(0x477a68) 0 'AOL' 1 'B' 2 '4RK11709' 3 20020702 4 3455 5 81.3588
So now I wanted to convert it into something that you would access using the foreach and split statements, so again if i were going through the debugger and wanted to see it, this is the format i would want it to be in:
0 'IBM,B,226110,20020702,545,12.5109' 1 'AOL,B,4RK11709,20020702,3455,12.5109'
How would you convert from the indexed-record array to the comma-separated array? Thanks in advance, Brent.

Replies are listed 'Best First'.
(jeffa) Re: Converting an index record array into a comma-separated array.
by jeffa (Bishop) on Jul 15, 2002 at 15:22 UTC
    join is the opposite of split:
    my @a = qw(IBM B 226110 20020702 545 12.5109); @a = join(',',@a); print "@a\n";
    But i usually store the results in a scalar, not an array, because join returns a scalar. Update: i did not realize you were working with an array of arrays when i answered ... sorry about that.

    jeffa

    L-LL-L--L-LL-L--L-LL-L--
    -R--R-RR-R--R-RR-R--R-RR
    B--B--B--B--B--B--B--B--
    H---H---H---H---H---H---
    (the triplet paradiddle with high-hat)
    
Re: Converting an index record array into a comma-separated array.
by japhy (Canon) on Jul 15, 2002 at 15:41 UTC
    The simplest approach is: $_ = join ',' => @$_ for @array; Or, written less idiomatically:
    for my $e (@array) { $e = join ",", @$e; }

    _____________________________________________________
    Jeff[japhy]Pinyan: Perl, regex, and perl hacker, who'd like a job (NYC-area)
    s++=END;++y(;-P)}y js++=;shajsj<++y(p-q)}?print:??;

Re: Converting an index record array into a comma-separated array.
by krujos (Curate) on Jul 15, 2002 at 15:21 UTC
    you could do something like
    foreach $element (@array) { $var = $var . $element . ","; }
    The dot operator appends, just like the + in java on strings. My example would give you an extra comma, but you could remove it with
    $array[@array -1]="";

    update added the part about the extra comma
      $array[@array -1]="";
      doesn't remove a trailing comma. What it does is replacing the last element of the array with an empty string.

      It's much better to not use an explicite loop, but to use join. Then you don't have to remove any trailing commas either.

      Abigail

Re: Converting an index record array into a comma-separated array.
by BrowserUk (Patriarch) on Jul 15, 2002 at 15:53 UTC

    Something like:

    #! perl -w use strict; use Data::Dumper; my @AoA = ( [ 'IBM','B', 226110, 22020702, 545 , 12.5109 ], [ 'AOL', 'B', '4RK11709', 20020702, 3455, 81.3588 ] ); my @AoS; for (0..1) { $AoS[$_] = join ',', @{$AoA[$_]} ; } print "Before\n", Dumper @AoA; print "After\n", Dumper @AoS; __DATA__ #output C:\test>181810 Before $VAR1 = [ 'IBM', 'B', 226110, 22020702, 545, '12.5109' ]; $VAR2 = [ 'AOL', 'B', '4RK11709', 20020702, 3455, '81.3588' ]; After $VAR1 = 'IBM,B,226110,22020702,545,12.5109'; $VAR2 = 'AOL,B,4RK11709,20020702,3455,81.3588';