Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

1 loop too many in array iteration

by jms53 (Monk)
on Feb 13, 2013 at 08:17 UTC ( #1018500=perlquestion: print w/ replies, xml ) Need Help??
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:
sub new { my ($class, %attr) = @_; my $ref = \%attr; my @history; $ref->{history} = \@history; }
I then have a method that pushes an array-ref to @{$ref->{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 -

Comment on 1 loop too many in array iteration
Select or Download Code
Re: 1 loop too many in array iteration
by Athanasius (Abbot) on Feb 13, 2013 at 08:23 UTC

    Square brackets [] return a reference to an array — i.e., a single scalar value. So the line:

    my @done = ["$time[5]-$time[4]-$time[3]", $type, $amount];

    assigns an array reference to the first element only ($done[0]) of @done. I think you wanted:

    my @done = ("$time[5]-$time[4]-$time[3]", $type, $amount);

    which populates the array @done with three scalar values.

    Hope that helps,

    Athanasius <°(((><contra mundum Iustus alius egestas vitae, eros Piratica,

Re: 1 loop too many in array iteration
by vinoth.ree (Prior) on Feb 13, 2013 at 09:27 UTC

    Athanasius++

    jms53 if the loop goes like this you try to print your data structure using Data::Dumper module, so that you can get understand how the data get saved.

      I didn't see the structure any different from what I was expecting in Data::Dumper, although that is likely to be my lack of experience with Data::Dumper.

      Athanasius's correction to the sub made everything work as expected.

      Thanks for all the help and wisdom!
      J -
Re: 1 loop too many in array iteration
by tmharish (Friar) on Feb 13, 2013 at 09:44 UTC

    Your new does not have a bless.

    Did you leave that out of the code fragment here?

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1018500]
Approved by Athanasius
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others having an uproarious good time at the Monastery: (3)
As of 2015-07-06 00:08 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (68 votes), past polls