The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

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
Perl: Getting back reference from s modifier cooldude Shell Programming and Scripting 8 03-19-2008 09:49 AM
how to get last value in an array in perl meghana Shell Programming and Scripting 7 02-04-2008 05:12 PM
split to array in perl jaganadh Shell Programming and Scripting 3 07-06-2007 06:29 AM
Help for record (2 dimensional array.) murtaza Shell Programming and Scripting 1 03-13-2007 08:10 PM

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 04-14-2003
photon's Avatar
photon photon is offline
Registered User
  
 

Join Date: Jul 2002
Posts: 162
Reference two dimensional array in Perl sub

I am trying to reference a two dimensional array in a
subroutine and can't seem to figure this one out in Perl.

Does anybody know? Please enlighten me.

Code:
#!/usr/bin/perl -w

use constant DIM => 4;

sub Shift_elements_right{
    my (@Input, @Output) = @_;
    for ($i = 0 ; $i <= DIM ; $i++ ){
	$Output[$i + 1] = $Input[$i];
    }
    $Output[1] = $Input[DIM];
}


# Initialize
for ($i = 0; $i <= DIM; $i++){
    $weight[0][$i] = $i;
}

# Shift call
for ($i = 0; $i <= DIM; $i++){
    Shift_elements_right(\@weight[$i], \@weight[$i + 1]);
}

# Print output.
for ($i = 0; $i <= DIM; $i++){
    for ($j = 0; $j <= DIM; $j++){
	print "Weight $i : $weight[$i][$j]\t";
    }
    print "\n";
}
  #2 (permalink)  
Old 04-15-2003
oombera's Avatar
oombera oombera is offline Forum Advisor  
Registered User
  
 

Join Date: Aug 2002
Location: Cleveland, OH
Posts: 804
Could you explain a little more about what you're trying to do?

I can pick out some of what you're doing, but I'm not sure why.. if you explain the what and why of your situation, it'll help us help you out..
  #3 (permalink)  
Old 04-15-2003
photon's Avatar
photon photon is offline
Registered User
  
 

Join Date: Jul 2002
Posts: 162
This is actually a more complex Pascal program I
am converting to Perl. The code given is a smaller
example of a larger program.

In pascal the program code looks like this:

Code:
CONST DIM = 4;
TYPE Vector = ARRAY [1..DIM] of REAL;
weights: ARRAY[1..DIM] of Vector;

PROCEDURE Initialize;
	VAR I: INTEGER;

	BEGIN
	FOR I := 1 TO DIM DO
		weights[1][I] := I;
	END



PROCEDURE Shift_elements_right (Input : Vector;
				VAR Output: Vector);
	VAR I: INTEGER;
	
	BEGIN
	FOR I := 1 TO DIM DO
		Output[I+1] := Input[I];
	Ouptut[1] := Input[DIM];
	END

BEGIN
FOR I := 1 TO DIM DO
	Shift_elements_right(weights[I], weights[I+1]);
END;

It is supposed to create a 4 x 4 dimensional array with only
the first row of vector being initialized.

My question is to reproduce this in Perl. I would like to
pass the array as a reference and put values in the 4 x 4
dimentional array through a subroutine.

Odviously I can do this without a subroutine but with a
larger program this is not practicle.
  #4 (permalink)  
Old 04-15-2003
oombera's Avatar
oombera oombera is offline Forum Advisor  
Registered User
  
 

Join Date: Aug 2002
Location: Cleveland, OH
Posts: 804
To initialize the two-dimensional array, you could do something like:
Code:
@array1 = ('0', '0', '0', '0');
@array2 = ('0', '0', '0', '0');
@array3 = ('0', '0', '0', '0');
@array4 = ('0', '0', '0', '0');

@bigArray = (\@array1, \@array2, \@array3, \@array4);
or to simplify, try this (I think it'll work):
Code:
@bigArray = ( ['0', '0', '0', '0'],
              ['0', '0', '0', '0'],
              ['0', '0', '0', '0'],
              ['0', '0', '0', '0']
);
Then to reference a particular value, you can use:
Code:
$bigArray[0][0]; # first row, first column
$bigArray[2][1]; # third row, second column
To store a value, use:
Code:
$bigArray[2][1] = '5'
or, in a subroutine:
Code:
for ($i = 0; $i < DIM; $i++){
    for ($j = 0; $j < DIM; $j++){
        bigArray[$i][$j] = $j; #assuming $j is the value you want to store...
    }
}
Let me know what works and what doesn't...
  #5 (permalink)  
Old 04-15-2003
photon's Avatar
photon photon is offline
Registered User
  
 

Join Date: Jul 2002
Posts: 162
The following code seems to solve my problem.

Code:
#!/usr/bin/perl -w

use constant DIM => 4;

sub Shift_elements_right{
    my ($ref) = shift;
    for($j = 1 ; $j <= DIM ; $j++){
	for ($i = 1 ; $i <= DIM ; $i++ ){
	    $ref->[$j + 1][$i + 1] = $ref->[$j][$i];
	}
    $ref->[$j + 1][1] = $ref->[$j][DIM];
    }
}


# Initialize
for ($i = 1; $i <= DIM; $i++){
	$weights[1][$i] = $i;
}

# Shift call
Shift_elements_right(\@weights);

# Print output.
for ($i = 1; $i <= DIM; $i++){
    for ($j = 1; $j <= DIM; $j++){
	print "Weight $i : $weights[$i][$j]\t";
    }
    print "\n";
}
  #6 (permalink)  
Old 04-16-2003
WIntellect's Avatar
WIntellect WIntellect is offline
Registered User
  
 

Join Date: Sep 2002
Location: United Kingdom
Posts: 170
Quote:
snip of sample code
FOR I := 1 TO DIM DO
weights[1][I] := I;
END
If you want to keep your Perl code similar to the original, I believe you can do things like the above, in this way:
Quote:
foreach $I (1..DIM) {
# process $I here
}
Closed Thread

Bookmarks

Tags
perl, perl shift, shift, shift perl

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 03:16 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