jms53 has asked for the wisdom of the Perl Monks concerning the following question:
Hello Monks,
Having followed your advice, my simple object is now working. However, I don't understand why the code works as it is.
I have an object, with the following structure:I then have a method that pushes an array-ref to @{$ref->{history}}:sub new { my ($class, %attr) = @_; my $ref = \%attr; my @history; $ref->{history} = \@history; }
sub trans { my @done = ["$time[5]-$time[4]-$time[3]", $type, $amount]; push @{$self->{history}}, \@done; }
My problem comes to when I want to see the contents of $self->{history}, I have to use 3 loops, when it's only a 2-dimensional array.
sub statement { #iterate through @{$self->{history}} in 2 dimensions my $self = shift; for my $index ( 0..$#{ $self->{history} } ) { for my $j (0..$#{$self->{history}[$index] } ) { for (0..$#{$self->{history}[$index][$j] } ) { print "$self->{history}[$index][$j][$_]\t"; } print "\n"; } } }
If anyone could explain to me why the need for 3 loops (or show me where my de-referencing went awfully wrong), it would be greatly appreciated.
J -
|
---|
Replies are listed 'Best First'. | |
---|---|
Re: 1 loop too many in array iteration
by Athanasius (Archbishop) on Feb 13, 2013 at 08:23 UTC | |
Re: 1 loop too many in array iteration
by tmharish (Friar) on Feb 13, 2013 at 09:44 UTC | |
Re: 1 loop too many in array iteration
by vinoth.ree (Monsignor) on Feb 13, 2013 at 09:27 UTC | |
by jms53 (Monk) on Feb 13, 2013 at 10:05 UTC |
Back to
Seekers of Perl Wisdom