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

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

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

 



Ratz
Deleted

Oct 27, 2000, 7:07 AM

Post #1 of 2 (786 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
[1,10,100,"a",5,2],
[2,10,100,"b",5,2],
[3,10,100,"c",5,2],
[4,10,100,"d",5,2],
[5,10,100,"e",5,2],

Here is the Script that reads from it:
$TMP='c:/scripts/ray.txt';

open (FILE,$TMP) | | die "cannot open file for read";
while (<FILE> )
{
push(@RAY,$_);}
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?

Sher


japhy
Enthusiast / Moderator

Oct 28, 2000, 10:24 PM

Post #2 of 2 (786 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";
</pre><HR></BLOCKQUOTE>

------------------
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