The UNIX and Linux Forums  


Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com



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

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 05-02-2004
zap zap is offline
Registered User
  
 

Join Date: Apr 2004
Posts: 6
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 (permalink)  
Old 05-03-2004
cbkihong cbkihong is offline Forum Advisor  
Advisor
  
 

Join Date: Sep 2002
Location: Hong Kong, China
Posts: 1,624
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 (permalink)  
Old 05-03-2004
zap zap is offline
Registered User
  
 

Join Date: Apr 2004
Posts: 6
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 (permalink)  
Old 05-03-2004
Optimus_P Optimus_P is offline Forum Advisor  
flim flam flamma jamma
  
 

Join Date: May 2001
Location: Chicago IL, USA
Posts: 1,006
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..
  #5 (permalink)  
Old 05-03-2004
cbkihong cbkihong is offline Forum Advisor  
Advisor
  
 

Join Date: Sep 2002
Location: Hong Kong, China
Posts: 1,624
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.
  #6 (permalink)  
Old 05-04-2004
zap zap is offline
Registered User
  
 

Join Date: Apr 2004
Posts: 6
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!
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 09:49 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0