Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Wanet Telecoms Ltd on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

loading data into 2d array 1

Status
Not open for further replies.

inncoggnitto

Programmer
Jun 19, 2001
77
US
i am pulling data from a db and need to push into a 2d array a column at a time. will loop through the following up to three time (so $k will be from 0 to 2). the sql statement will pull one set of data at a time out and there can be up to three recordsets in the results. i need to get the data into the array to put into html table later on. i am getting the following error. "can't use subscript on array slice near q]. the following is what i thought would work. only occasional perl programmer (sorta) so be kind.

thanks
Code:
$sth = $dbh->prepare("SELECT * from PSTCOMP WHERE PSTCOMP.FACID = '$requested_fac_id'  AND PSTCOMP.UNIT_ID = '$unit_id' ORDER BY PSTCOMP.TANK_ID, PSTCOMP.COMPART_LETTER");

	$sth->execute();
	while ($fields = $sth->fetchrow_arrayref()) {
		push @comp, [@$fields]; .
		$rowcount_comp++;
	}
#this is where i am trying to read the one dim array into a 2 dim where $k is the column number
	for ($q=0; $q=$#comp; $q++){
		@comp_ary[$k][$q] = $comp[$q];
	}
 
There are a couple of problems that I can see. The actual error you're getting is because [tt]@comp_ary[$k][$q][/tt] should be [tt]$comp_ary[$k][$q][/tt].

I think that you also need to change:
[tt]push @comp, [@$fields];[/tt]
to:
[tt]push @comp, @$fields;[/tt]

So you're pushing the elements of the array, rather than pushing a reference to an anonymous array containing the elements of the array.

The last problem that I can see, offhand, is that [tt]$q=$#comp[/tt] in the second portion of the [tt]for[/tt] statement should be [tt]$q <= $#comp[/tt]
 
when i do the first suggestion i gen a new error msg of &quot;type of arg 1 to push must be array(not array element)&quot;

suggestion 2 - the way i have it works (i use it in other scripts) and i got it from the programming perl dbi book on pg 114

i agree with three

just need to get past one still

thanks
 
i am trying to read the one dim array into a 2 dim

@comp is not a 1D array, it's 2D. Each element in the array is a refence to a row array. So $comp[$q] is a reference to the array of the qth row of fields from your DB. This make @comp_ary a 3D array.

If you are going to assign $comp[$q] to $comp_ary[$k][$q] you don't need to loop over $q to do it. You can just do
Code:
@{$comp_ary[$k]} = @comp;

jaa
 
i have tried all suggestions and others from a different source and still all i get out is an array reference in hex.

this is what it looks like (at aleast the last iteration)
+++++++++++++++++++++++++++++++++++++++++++++++++code
#this is the sub to get the data into the array called from the sub below
sub Compartment {
$rowcount_comp = 0;
@comp = ();

$sth = $dbh->prepare(&quot;SELECT * from PSTCOMP WHERE PSTCOMP.FACID = '$requested_fac_id' AND PSTCOMP.UNIT_ID = '$unit_id' ORDER BY PSTCOMP.TANK_ID, PSTCOMP.COMPART_LETTER&quot;);
$sth->execute();

while ($fields = $sth->fetchrow_arrayref()) {
push @comp, [@$fields]; #Create copy of array reference. See DBI book pp 113-114.
$rowcount_comp++;
}
for ($q=0; $q<=$#comp; $q++){
# @{$comp_ary->[$k]} = @comp;
$comp_ary->[$k]->[$q] = $comp[$q];
}

#CompDetailsOut($rowcount_comp, \@comp);
$sth->finish;
}

#the first for loop sets the id for the tank and is used in the sql above to get all compartment data, up to 3 comps possible. second loop is supposed to pull data out of array
all i have been able to get out is the reference? ARRAY(0x4025475c), but not the value. i get either the ref or blank regardless of what i try.

#the z output is just for debugging so i can see the counter working. the attachement is a little of the output. pg 273 programming perl3 shows print $AoA[1][2] as the way to specify a specific value for output. the -> thing doesn't change anything in the results good or bad, when i tried it in both subs.

sub CompDetailsOut{
for ($k=0; $k < 3; $k++){
$unit_id = $u_id[$k];
Compartment();
}
$z=0;
for ($k=0; $k < $max_count; $k++){
if (k != 0 ) {
$z++;
}

print qq~
<tr>
<td class=&quot;border2&quot; width=&quot;180&quot;>Compartment Letter:
</td>
<td class=&quot;border2&quot; width=&quot;180&quot;>$comp_ary->[0]->[$z] z=$z
</td>
<td class=&quot;border2&quot; width=&quot;180&quot;>$comp_ary->[1]->[$z] z=$z
</td>
<td width=&quot;180&quot;>$comp_ary->[2]->[$z] z=$z
</td>
</tr>
<tr>
<td class=&quot;border2&quot;>Capacity:
</td>
<td class=&quot;border2&quot;>$comp_ary[$z++][0] z=$z
</td>
<td class=&quot;border2&quot;>$comp_ary[$z][1] z=$z
</td>
<td>$comp_ary[$z][2] z=$z
</td>
</tr>

etc.+++++++++++++++++++++++code end

putting print qq~<h1>$comp[0], $comp[1], $comp[2], $comp[3], $comp[4], $comp[5]</h1>~; into the loop and exiting yeilds
ARRAY(0x4025b468), , , , ,

and if i do a full printout to my output page i get the following for the first sets of data. (looks better in html of course)

Compartments
Compartment Letter: ARRAY(0x4025475c) z=0 ARRAY(0x401a9624) z=0 ARRAY(0x40254184) z=0
Capacity: z=1 z=1 z=1
Substance Stored: z=2 z=2 z=2
Other Substance Stored: z=3 z=3 z=3
Tank Release Detection
I: z=4 z=4 z=4
II: z=5 z=5 z=5
III: z=6 z=6 z=6
Other: z=7 z=7 z=7
Variance:

 
I still believe that
[tt]while ($fields = $sth->fetchrow_arrayref()) {
push @comp, [@$fields]; #Create copy of array reference. See DBI book pp 113-114.
$rowcount_comp++;
}
[/tt]

Should be:
[tt]while ($fields = $sth->fetchrow_arrayref()) {
push @comp, @$fields; #Create copy of array reference. See DBI book pp 113-114.
$rowcount_comp++;
}[/tt]

You're pushing an array reference onto the end of [tt]@comp[/tt], not pushing the values of the columns.
 
you are correct, i must have missed the dropping the [] part. wonder why it acts differently in this case than in the rest of my script, as this is only a small part of the whole, i have 9 other sql stmts pulling data out.

i thank you.

now all i have to do is to make sure the looping and other stuff i have going on is getting the correct data out.

that is for next week though, time to go home this end.

thanks again
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top