in reply to Re: Where is my foreach data going to?
in thread Where is my foreach data going to?

Well, if I change the algorithm to :
$qry = $DB->Query("SELECT * FROM recipes WHERE type = '$type' ORDER +BY rand() limit $quantity") || die "Got error on select: $Mysql::db_errstr\n"; + for ($i=0; $i<$qry->numrows; $i++) { @row = $qry->fetchrow or warn "recipes didn't find a matching row" +; print "\@row is assigned @row\n"; $j = 0; foreach my $bug (@row) { $athlete[$i][$j++] = $bug ; print "\$i\$j is assigned $i:$j\n"; print "\$athlete[\$i,\$j] is assigned the value $athlete[$i][$j] +\n"; print "\$bug is assigned the value $bug\n"; } } }
then it yields:
@row is assigned 33 Small Bunch Grapes carb 15 0.4 0.1 $i$j is assigned 4:1 Use of uninitialized value in concatenation (.) or string at ./getCurr +entNeeds.pl line 74. $athlete[$i,$j] is assigned the value $bug is assigned the value 33 $i$j is assigned 4:2 Use of uninitialized value in concatenation (.) or string at ./getCurr +entNeeds.pl line 74. $athlete[$i,$j] is assigned the value $bug is assigned the value Small Bunch Grapes $i$j is assigned 4:3 Use of uninitialized value in concatenation (.) or string at ./getCurr +entNeeds.pl line 74. $athlete[$i,$j] is assigned the value $bug is assigned the value carb $i$j is assigned 4:4 Use of uninitialized value in concatenation (.) or string at ./getCurr +entNeeds.pl line 74. $athlete[$i,$j] is assigned the value $bug is assigned the value 15 $i$j is assigned 4:5 Use of uninitialized value in concatenation (.) or string at ./getCurr +entNeeds.pl line 74. $athlete[$i,$j] is assigned the value $bug is assigned the value 0.4 $i$j is assigned 4:6 Use of uninitialized value in concatenation (.) or string at ./getCurr +entNeeds.pl line 74. $athlete[$i,$j] is assigned the value $bug is assigned the value 0.1

Replies are listed 'Best First'.
Re: Re: Re: Where is my foreach data going to?
by Enlil (Parson) on Sep 18, 2003 at 01:26 UTC
    The error lies in the fact that you are incrementing $j here:
    $athlete[$i][$j++] = $bug;
    The problem is that since $j is one larger than before, and thus you are not printing out the same thing (2 lines later) as what you just set. (ie $athlete[0][0] != $athlete[0][1]) so you get the warning.

    you might want to increment the $j by one at the end of the loop.

    update: I am afraid, I might have confused you so here goes. Let's pretend we are going through the inner loop on the first pass:

    $j = 0; foreach my $bug (@row) { #here $j == 0; so the following line would be #$athlete[$i][0] = $bug; $athlete[$i][$j++] = $bug #at this poing the $j == 1 because of the $j++ #on the previous line. print "\$i\$j is assignt $i:$j\n"; print "\@athlete[\$i,\$j] is assigned the value $athlete[$i][$j]\n"; #on the previous line you $j == 1 so you are refering to #$athlete[$i][1] which is not the same as what you set #but rather the value you will set the next time through. . . .
    if you change the print line to:
    print "\@athlete[\$i,\$j] is assigned the value $athlete[$i][$j-1]\n";
    you would print the value you just set, which is what I think you are trying to do.

    -enlil

Re: Re: Re: Where is my foreach data going to?
by Roger (Parson) on Sep 18, 2003 at 01:33 UTC
    Try this instead...
    $i = 0; while (@row = $qry->fetchrow) { print "\@row is assigned @row\n"; $j = 0; foreach my $bug (@row) { $athlete[$i][$j] = $bug ; print "\$i\$j is assigned $i:$j\n"; print "\$athlete[\$i,\$j] is assigned the value $athlete[$i][$j] +\n"; print "\$bug is assigned the value $bug\n"; $j++; } $i++; }