Reading data into muti-dimentional array - in perl


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Reading data into muti-dimentional array - in perl
# 1  
Old 05-02-2004
Reading data into muti-dimentional array - in perl

Just want to learn how these are read into array but I don't seem to get it right what do I go wrong?
Below is the sample

Thanks


input

1 2 3 4
5 6 7 8
9 1 2 3
4 5 6 7

#!/usr/bin/perl

open (InFILE,"input");

while (<InFILE>) {

@ar = split ;

$arref = \@ar;

push(@multiarray,$arref);

}

for ($i=0;$i<=4;$i++){
for ($j=0;$j<=4;$j++){

print " $multiarray[$i][$j] ";
}
# 2  
Old 05-03-2004
Re: Reading data into muti-dimentional array - in perl

I don't really know why you put your program like this as it seems really awkward, but this seems like this may do you want:

Quote:
Originally posted by zap
#!/usr/bin/perl

open (InFILE,"input");

while (<InFILE> ) {

my @ar = split ;

$arref = \@ar;

push(@multiarray,$arref);

}

for ($i=0;$i<4;$i++){
for ($j=0;$j<4;$j++){

print " $multiarray[$i][$j] ";
} }
Output:

1 2 3 4 5 6 7 8 9 1 2 3 4 5 6 7


Hint: the "my" is crucial here --- you have made your program unnecessarily complex that you have actually set for yourself a subtle trap without your realizing it that is tedious to locate. Why "my" is needed here is left for your exploration to encourage you to find out the answer yourself.
# 3  
Old 05-03-2004
cbkihong,

Thanks it did work. I am very new to perl and I am trying to understand how multi-dimentiona array is read in.
Interesting about "my" I will investigate further!
Zap
# 4  
Old 05-03-2004
what you might want to take a look at is:

Code:
Learning Perl Objects, References & Modules.
5.2. Viewing Complex Data with Data :: Dumper

I used data dumper on your script above and this is what i get.

$VAR1 = [
          '1',
          '2',
          '3',
          '4'
        ];
$VAR1 = [
          '5',
          '6',
          '7',
          '8'
        ];
$VAR1 = [
          '9',
          '1',
          '2',
          '3'
        ];
$VAR1 = [
          '4',
          '5',
          '6',
          '7'
        ];

i think i know what your asking and if that is the case this is what is happening.

Code:
LINE: 1 )while (<InFILE> ) {
LINE: 2 )
LINE: 3 )@ar = split ;
LINE: 4 )
LINE: 5 )$arref = \@ar;
LINE: 6 )
LINE: 7 )push(@multiarray,$arref);
.....
....

line 1 says read each line in the file.
line 3 says for each space add a new element to @ar for each items read in $_. (IE: for the first line read in @ar=qw(1 2 3 4)Smilie
line 7 says for each entry to @ar create an anonymous array inside @multiarray. (IE:
@mulriarray=(
[1,2,3,4];
);
)


if you are trying to do something else be as detailed in your question as possable and i am sure we can get you sorted out and pointing in the right direction.

Last edited by Optimus_P; 05-03-2004 at 03:42 PM..
# 5  
Old 05-03-2004
I decided to unveil the trap.

Without the "my", the result I got (with the original script) was

4 5 6 7 4 5 6 7 4 5 6 7 4 5 6 7

(4 5 6 7 printed four times). Shouldn't get the first three lines printed at all because all four references saved into @multiarray point to the same symbol table entry, which always refers to the most recent anonymous list read (each assignment to @ar overwrites the previous entry). That explains why four consecutive 4 5 6 7 is resulted. The line read from file is correct but the output doesn't.

With @ar declared "my", it becomes a lexical array variable which gets expired on each loop iteration (so at the time the block ends its reference count drops to 1 -- the only reference left is the one saved in @multiarray). Assignment to @ar in the next iteration no longer affects the previous saved reference because it is now a different variable from the earlier @ar.

That's why I said the trap is a subtle one that is not obvious from the surface. Smilie
# 6  
Old 05-04-2004
Thanks for the response and expalination.

I kind of understand the scoping of the array using "my" and yes it seems incredably tricky with referencing.

I never have thought that it would use the same addresses eventhough the content changed.

I'll play around abit more to gain more understanding of it!
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to embed data instead of reading user input from an array?

Hello, I am running under ubuntu1 14.04 and I have a script which is sending given process names to vanish so that I'd see less output when I run most popular tools like top etc in terminal window. In usual method it works. Whenever I restart the system, I have to enter the same data from... (2 Replies)
Discussion started by: baris35
2 Replies

2. Shell Programming and Scripting

Perl : reading data from dumper variable

Hi team, # PERL I have Dumper variable in perl and containing the below data and trying to fetch value and name from the reference variable. $VAR1 = { 'retainSysIds' => 'true', 'variables' => , 'name' => , ... (4 Replies)
Discussion started by: giridhar276
4 Replies

3. Shell Programming and Scripting

Perl :: reading values from Data Dumper reference in Perl

Hi all, I have written a perl code and stored the data into Data structure using Data::Dumper module. But not sure how to retreive the data from the Data::Dumper. Eg. Based on the key value( Here CRYPTO-6-IKMP_MODE_FAILURE I should be able to access the internal hash elements(keys) ... (1 Reply)
Discussion started by: scriptscript
1 Replies

4. Shell Programming and Scripting

Perl : Large amount of data put into an array

This basic code works. I have a very long list, almost 10000 lines that I am building into the array. Each line has either 2 or 3 fields as shown in the code snippit. The array elements are static (for a few reasons that out of scope of this question) the list has to be "built in". It... (5 Replies)
Discussion started by: sumguy
5 Replies

5. Shell Programming and Scripting

reading data from a file to an array

I need some help with this code below, i doesnt know why it will run twice with my function, but my function only got if else, any other way that can read line and put into array? while read line; do read -A array <<<$line n=${#array} for ((i=1;i<$n;i++)); do print... (1 Reply)
Discussion started by: gavin_L
1 Replies

6. Shell Programming and Scripting

Reading from a file and assigning to an array in perl

I wrote a simply perl that searched a file for a particualr value and if it found it, rite it and the next three lines to a file. Now I have been asked to check those next three lines for a different value and only write those lines if it finds the second value. I was thinking the best way to... (1 Reply)
Discussion started by: billprice13
1 Replies

7. Shell Programming and Scripting

Perl Script for reading table format data from file.

Hi, i need a perl script which reads the file, content is given below. and output in new file. TARGET DRIVE IO1 IO2 IO3 IO4 IO5 ------------ --------- --------- --------- --------- --------- 0a.1.8 266 236 ... (3 Replies)
Discussion started by: asak
3 Replies

8. Shell Programming and Scripting

Storing data in perl 2D array

Respected All, Kindly help me out. I have got file listings in a directory like this: -rw-r--r-- 1 root root 115149 2011-11-17 07:15 file1.stat.log -rw-r--r-- 1 root root 115149 2011-11-18 08:15 file2.stat.log -rw-r--r-- 1 root root 115149 2011-11-19 09:15 file3.stat.log -rw-r--r-- 1... (2 Replies)
Discussion started by: teknokid1
2 Replies

9. Shell Programming and Scripting

Perl: Reading data from other file

Hi, I am writting some perl scripts for daily backup process. In which I want to pass some data/referance from another txt file. Text file contains only one column and multiple rows. I want to pass this data to variables of another perl script. e.g. Refdoc.txt file contains data as: perl1... (3 Replies)
Discussion started by: n.dba
3 Replies

10. Shell Programming and Scripting

PERL: reading 2 column data into Hash file

I am trying to read in a 2 column data file into Perl Hash array index. Here is my code. #!/usr/bin/perl -w use strict; use warnings; my $file = "file_a"; my @line = (); my $index = 0; my %ind_file = (); open(FILE, $file) or die($!); while(<FILE>) { chomp($_); if ($_ eq '') { ... (1 Reply)
Discussion started by: subhap
1 Replies
Login or Register to Ask a Question