in reply to Where is my foreach data going to?

OK, here's a reply to my own post. But it should reveal why I don't think it's a problem with data types. This will trim out some of the debugging code, but what you'll see it that only an occasional assignment is not made:
@row is assigned 31 Medium Orange carb 14 2 0.2 @athlete[$i,$j] is assigned the value @athlete[$i,$j] is assigned the value Medium Orange @athlete[$i,$j] is assigned the value carb @athlete[$i,$j] is assigned the value 14 @athlete[$i,$j] is assigned the value 2 @athlete[$i,$j] is assigned the value 0.2 @row is assigned 42 1/2 c. (cooked) past carb 19.5 3.2 0.4 @athlete[$i,$j] is assigned the value 42 42 @athlete[$i,$j] is assigned the value @athlete[$i,$j] is assigned the value carb @athlete[$i,$j] is assigned the value 19.5 @athlete[$i,$j] is assigned the value 3.2 @athlete[$i,$j] is assigned the value 0.4 @row is assigned 26 1/2 c. canned peache carb 14 1 0 @athlete[$i,$j] is assigned the value 26 0.4 @athlete[$i,$j] is assigned the value 1/2 c. canned peache 1/2 c. cann +ed peache @athlete[$i,$j] is assigned the value @athlete[$i,$j] is assigned the value 14 @athlete[$i,$j] is assigned the value 1 @athlete[$i,$j] is assigned the value 0 @row is assigned 6 White Bread III carb 24.5 4.1 2.3 @athlete[$i,$j] is assigned the value 6 @athlete[$i,$j] is assigned the value White Bread III 0 @athlete[$i,$j] is assigned the value carb carb @athlete[$i,$j] is assigned the value @athlete[$i,$j] is assigned the value 4.1 @athlete[$i,$j] is assigned the value 2.3 @row is assigned 20 Marias Spanish Rice carb 42.4 4.3 5 @athlete[$i,$j] is assigned the value 20 @athlete[$i,$j] is assigned the value Marias Spanish Rice @athlete[$i,$j] is assigned the value carb 2.3 @athlete[$i,$j] is assigned the value 42.4 42.4 @athlete[$i,$j] is assigned the value @athlete[$i,$j] is assigned the value 5
See what I mean? It just seems to almost be caching or writing out of order. I'm sure it's my mistake, but I sure don't know why it's doing this. These are the results from my original code base.

Replies are listed 'Best First'.
Re: Re: Where is my foreach data going to?
by Roger (Parson) on Sep 18, 2003 at 02:03 UTC
    It has everything to do with data types! @athelete[$i,$j], $athelete[$i, $j] and $athelete[$i][$j] are completely different. Try to use the Data::Dumper to investigate why:
    use strict; use Data::Dumper; my @array; for (my $i=0;$i<2;$i++) { for (my $j=0;$j<2;$j++) { @array[$i, $j] = "$i.$j"; } } print "Case 1...\n"; print Dumper(@array); for (my $i=0;$i<2;$i++) { for (my $j=0;$j<2;$j++) { $array[$i][$j] = "$i . $j"; } } print "Case 2...\n"; print Dumper(@array); for (my $i=0;$i<2;$i++) { for (my $j=0;$j<2;$j++) { $array[$i, $j] = "$i . $j"; } } print "Case 3...\n"; print Dumper(@array);
    You will get -
    Case 1... $VAR1 = undef; $VAR2 = undef; Case 2... $VAR1 = [ '0 . 0', '0 . 1' ]; $VAR2 = [ '1 . 0', '1 . 1' ]; Case 3... $VAR1 = '1 . 0'; $VAR2 = '1 . 1';
    As you can see, only case 2 gives you what you want, a two dimensional array.
      Just one point , when using Data::Dumper::Dumper() to dump an array or a hash, it is often better to pass a reference to the hash/array, as the output looks more like the original data structure - and you avoid the copying overhead of call-by-value vs call-by-refernce
      use Data::Dumper; my @array = (1,2,3,4,5,6,7); print Dumper(\@array); my %hash; # below is a hash slice - you should learn about these @hash{@array} = @array;# a hash whose keys == its values print Dumper(\%hash);
      results in the output
      $VAR1 = [ 1, 2, 3, 4, 5, 6, 7 ]; $VAR1 = { '7' => 7, '1' => 1, '2' => 2, '3' => 3, '4' => 4, '5' => 5, '6' => 6 };
      which to my eyes seems nicer

        which to my eyes seems nicer

        And more accurate too. If the data structure was complex then the end result would be extremely confusing. Especially with Purity(1).


        ---
        demerphq

        <Elian> And I do take a kind of perverse pleasure in having an OO assembly language...