Re: Perl and array of array's


Subject: Re: Perl and array of array's
From: Arthur Corliss (arthur@corlissfamily.org)
Date: Tue Aug 13 2002 - 14:00:12 AKDT


> I am reading a file that has this:
>
> 1,2,4,5,67,45
> 2,3,43,5,3,2
> 34,54,23,4,1,1
>
> I would like to do two things....read the first line in the file and
> put it in an array. Then take that array and stick it in an array.
>
> I then want to print out array[2][2] and the response should be 54.

Actually, array[2][2] would be 23. Keep in mind that all indices in Perl are
zero-based.

> Can someone give me a leg up on this?
>
> I can do it when I set the array by hand, but when I read it from a
> file, for some reason I can't get it right...

while (defined($line = <FILE>)) {
  chomp $line;
  push(@array, [ split(/\s*,\s*/, $line) ]);
}

The \s* is optional, of course, if you're sure none of the CSV data has
whitespace padding.

        --Arthur Corliss
          Bolverk's Lair -- http://arthur.corlissfamily.org/
          Digital Mages -- http://www.digitalmages.com/
          "Live Free or Die, the Only Way to Live" -- NH State Motto

---------
To unsubscribe, send email to <aklug-request@aklug.org>
with 'unsubscribe' in the message body.



This archive was generated by hypermail 2a23 : Tue Aug 13 2002 - 14:45:22 AKDT