[Date Prev][Date Next][Thread Prev][Thread Next] [Search] [Date Index] [Thread Index]

Re: [MacPerl-WebCGI] Can you read a file "vertically"?



>Hi all:
>
>Question: Can a file be read / parsed / coded "vertically"? Let me explain:
>
>If this is a comma delimited file:
>        do,re,mi
>        fa,sol,la
>        si,de,me
>..I just learned how to end up with three arrays:
>    @array1 = (do,re,mi)
>    @aray2 = (fa,sol,la)
>    @array3 =(si,de,me);
>
>Q: is there a way (and what is it) to end up with these three arrays:
>    @array1 = (do,fa,si)
>    @aray2 = (re,sol,de)
>    @array3 =(mi,la,me) ?
>
>Thank you,
>
>Riccardo Perotti


Dear Riccardo,

what about using a matrix, or a list of lists (see perlLoL.pod). For example:

______________
#!perl -w

 @array1 = ('do','re','mi');
 @array2 = ('fa','sol','la');
 @array3 = ('si','de','me');
                                # (Row, Column)
 @matrix = (['do','re','mi'],   # (0,0)  (0,1) (0,2)
            ['fa','sol','la'],  # (1,0)  (1,1) (1,2)
            ['si','de','me']);  # (2,0)  (2,1) (2,2)


print "(1,2) = $matrix[1][2] \n"; # prints 'la'
print "(2,0) = $matrix[2][0] \n"; # prints 'si'

# or

@matrix2 = ( \@array1,
             \@array2,
             \@array3);

print "(1,2) = $matrix2[1][2] \n"; # prints 'la'
print "(2,0) = $matrix2[2][0] \n"; # prints 'si'

# let's go vertical

for ($i = 0; $i <= 2; $i++) {
  push (@vert_array0, $matrix2[$i][0])
}#for

print @vert_array0;
print "\n";

# or

foreach $ref (@matrix2) {
   push (@vert_array1, $ref->[0]);
   push (@vert_array2, $ref->[1]);
   push (@vert_array3, $ref->[2]);
}#for

print @vert_array1;
print "\n";
print @vert_array2;
print "\n";
print @vert_array3;
print "\n";
______________

Hope that helps.

Best regards

--Thomas







==== Want to unsubscribe from this list?
==== Send mail with body "unsubscribe" to macperl-webcgi-request@macperl.org