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