
August 11th, 2006, 04:44 PM
|
|
Contributing User
|
|
Join Date: Apr 2004
Posts: 41
Time spent in forums: 7 h 8 m 47 sec
Reputation Power: 5
|
|
|
Array subscript in perl script on AS400
Hello everybody,
We're trying to break a string file into fields for our DB2 using perl script. Here is a piece of code that gives us trouble:
PHP Code:
if (@fields[1] eq 'SDQ')
{ # Detail record processing.
my $l = scalar @fields; # Get the number of records in this collection (max 10 pairs)
printf(OUT "%2s%2s", (@fields[2], @fields[3])); # Add the two core fields "UNITS" and the "ID"
for ($k = 4; $k < $l; $k += 2){ # Set up a loop to go through the store/units pair values
printf(OUT ">%4s%3s", (@fields[$k], @fields[$k+1])); # Add the STORE and UNITS values to the string for each one present
}
print(OUT "\n"); # Add a line feed to the end of the detail record
#die;
}
It works fine on PC and gives out the right values. However, the same code on AS400 outputs the incorrect results. It looks like $k in the for-loop is not evaluated correctly or is not evaluated at all.
On PC, @fields[$k] gives a string and on AS400 produces blank(undef).
If on AS400 we hardcode each iteration like :
PHP Code:
for ($k = 4; $k < $l; $k +=2){ # Set up a loop to go
if ($k == 4){
printf(OUT "%4s%3s", (@fields[4], @fields[5]));}
if ($k == 6){
printf(OUT "%4s%3s", (@fields[6], @fields[7]));}
if ($k == 8){
printf(OUT "%4s%3s", (@fields[8], @fields[9]));}
if ($k == 10){
printf(OUT "%4s%3s", (@fields[10], @fields[11]));}
if ($k == 12){
printf(OUT "%4s%3s", (@fields[12], @fields[13]));}
etc…
}
...then it gives the same output as if it ran on PC.
Is there anything specific about loops in perl script when running on AS400?
Thank you very much.
|