![]() |
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.
|
|
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 |
| 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 |
![]() |
|
|
LinkBack | Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
|
|
|||||
|
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. |
|
|||||
|
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);
Code:
@bigArray = ( ['0', '0', '0', '0'],
['0', '0', '0', '0'],
['0', '0', '0', '0'],
['0', '0', '0', '0']
);
Code:
$bigArray[0][0]; # first row, first column $bigArray[2][1]; # third row, second column Code:
$bigArray[2][1] = '5' 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...
}
}
|
|
|||||
|
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";
}
|
|
|||||
|
Quote:
Quote:
|
![]() |
| Bookmarks |
| Tags |
| perl, perl shift, shift, shift perl |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|