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] ";
}

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:

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.

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

what you might want to take a look at is:

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.

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):wink:
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.

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. :wink:

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!