![]() |
|
|
google unix.com
|
|||||||
| Forums | Register | Forum Rules | Links | Albums | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read |
| Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here. |
More UNIX and Linux Forum Topics You Might Find Helpful
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| perl -write values in a file to @array in perl | meghana | Shell Programming and Scripting | 27 | 06-07-2009 06:05 PM |
| sorting data using array in ksh | ali560045 | Shell Programming and Scripting | 4 | 12-04-2007 04:26 AM |
| reading reading data from webpage | phani_sree | High Level Programming | 3 | 11-01-2007 01:28 PM |
| Clearing Data in an Array | Octal | High Level Programming | 3 | 05-16-2007 06:19 PM |
| Using loop reading a file,retrieving data from data base. | Sonu4lov | Shell Programming and Scripting | 1 | 01-19-2007 03:38 AM |
![]() |
|
|
LinkBack | Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
|
|
||||
|
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] "; } |
|
||||
|
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:
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. |
|
||||
|
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) ![]() 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.. |
|
||||
|
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. ![]() |
|
||||
|
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! |
![]() |
| Bookmarks |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|