Sunday, 18 August 2013

Concatenating strings from a multidimensional array overwrites the target string in Perl

Concatenating strings from a multidimensional array overwrites the target
string in Perl

I've built a two dimension array a row at a time and populated all the
cells with string values. There are always 12 columns but the number of
rows vary. Now I'd like to build a string to pump to a database of each
row but when I run the following code:
$outstring = "";
for ($i=0; $i < $ctrLASTROW + 1; $i++) {
for ($k=0; $k < 12; $k++){
$datastring = $DATATABLE[$i][$k]);
$outstring .= $datastring;
}
}
$outstring takes the first value. Then on the second inner loop and
subsequent loops the value in $outstring gets overlaid. For example the
first value is "DATE" then the next time when the value "ABC" gets fed to
it. Rather than being the hoped for "DATEABC" it's "ABCE". I figure I'm
missing the scalar / list issue but I've tried who knows how many
variations to no avail. When I first started I tried the concatenation
directly from the DATATABLE. Same problem. Only quicker.

No comments:

Post a Comment