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

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.

Replies are listed 'Best First'.
Re: Re: Re: Where is my foreach data going to?
by leriksen (Curate) on Sep 18, 2003 at 06:37 UTC
    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...