Contact US

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

Reading a 2-D array with File handle

Reading a 2-D array with File handle

Reading a 2-D array with File handle

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?


RE: Reading a 2-D array with File handle

The data you have in ray.txt is 6 entries in a row. So what do you mean by 2-D array???.

Give this code a try. Hope it works. $my2darr is hash, whose keys are 1,2,3,... . And the values are the rest of the entries on each line. This represents a 2D array.
To manipulate this hash use $my2darr->{key}, unlike $my2darr{key}.

use Data::Dumper;
my $my2darr ={};
while (<DATA>)
print Data::Dumper->Dump([$my2darr],['MY2DARR']);

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members! Already a Member? Login

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close