On Mar 16, 2004, at 10:13 AM, Joseph Paish wrote:

following the example in the perldsc (data structures cookbook), i wrote this
piece of code to create an array of arrays from a data file :



#!/usr/bin/perl -w use strict ;

my @AofA ; # array of arrays of file contents
open (fh1, "</path/to/filename/filename.txt") or die ("no can do") ;
while (<fh1>) {
    push @AofA, (split /\t/) ;

That line should read:


push @AofA, [ split m/\t/ ]; # brackets, not parens

The brackets create an array reference to be pushed onto the original array.

}

close (fh1) ;

print $AofA[0] ;
print "\n" ;

Two arrays means two dimensions or two indexes:


print "$AofA[0][0]\n"; # the first column of the first row

# or for all elements

foreach (@AofA) {
        print "@$_\n";
}

Hope that helps.

James

# this gives me "cho" when what i wanted was "cho   249   837"
# later, i was hoping to use $AofA[0][0] to access "cho"

an example of the file follows (fields are separated by tabs in "real life")
cho 249 837
abc 123 456
abc 984 235
nurb 246 973



it appears the problem is where i am doing the "push" while reading the file.


suggestions?

thanks

joe


-- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] <http://learn.perl.org/> <http://learn.perl.org/first-response>





--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>




Reply via email to