CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
Search Posts SEARCH
Who's Online WHO'S
Log in LOG

Home: Perl Programming Help: Advanced:
Reading 2-D Array from file handle



Oct 27, 2000, 7:07 AM

Post #1 of 2 (1955 views)
Reading 2-D Array from file handle Can't Post

I have a script that works fine when the data is stored in the same file as the script.

The data is a 2-D array.

because the file is quite large, I stored it in a seperate .txt file to read only from the script.

But the script does not 'see' the data as a 2-d array, as it can't access specific elements.

I have scaled it down to show you what I mean:
Here data in ray.txt

Here is the Script that reads from it:

open (FILE,$TMP) | | die "cannot open file for read";
while (<FILE> )
close FILE;
#test if array has been created.
print "@RAY\n"; #prints OK
$num=scalar(@RAY); #returns 5
print "$num\n";
$row=($RAY[1]->[3]);#does not work
print "$row\n";
my $var=($RAY[1]->[2]);#does not work
print "$var\n";

The exact script works OK if the data is in the same file as the script, once the data is read, the script should work as normal??right?


Enthusiast / Moderator

Oct 28, 2000, 10:24 PM

Post #2 of 2 (1955 views)
Re: Reading 2-D Array from file handle [In reply to] Can't Post

No, just push the lines of the file to an array will not make a two-dimensional array. It will make an array of strings. However, if your data file is written as proper Perl, you should be able to just say:

<BLOCKQUOTE><font size="1" face="Arial,Helvetica,sans serif">code:</font><HR>

@two_d_array = require "configfile.txt";

Jeff "japhy" Pinyan -- accomplished author, consultant, hacker, and teacher


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives