×
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

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

(OP)
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

RE: Reading a 2-D array with File handle

Hai,
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;
$Data::Dumper::Indent=1;
my $my2darr ={};
while (<DATA>)
{
    chomp;
    s/(\[|\])//g;
    split(",",$_,9999);
    push(@{$my2darr->{$_[0]}},@_[1..$#_]);
}
print Data::Dumper->Dump([$my2darr],['MY2DARR']);
__DATA__
[1,10,100,"a",8,10]
[2,10,100,"b",8,10]
[3,10,100,"c",8,10],
[4,10,100,"d",8,10],
[5,10,100,"e",8,10],

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