Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

Re: Nested Loops vs Good programming

by jwkrahn (Abbot)
on Nov 14, 2008 at 13:08 UTC ( [id://723642]=note: print w/replies, xml ) Need Help??


in reply to Nested Loops vs Good programming

I don't know about your stated problem, but your loops have an off-by-one error:

my $count = @{ $doc->{$sub1} }; print "\n$sub1\t"; for (my $i=0; $i <= $count; $i++) {

Should be:

my $count = $#{ $doc->{$sub1} }; print "\n$sub1\t"; for my $i ( 0 .. $count ) {

The same applies to $count2 and $count3.

Replies are listed 'Best First'.
Re^2: Nested Loops vs Good programming
by ikegami (Patriarch) on Nov 14, 2008 at 20:05 UTC
    $count is a misnomer in your new code. If you change what a variable contains, you need to change its name too.

    Minimal fix (<=<):

    my $count = @{ $doc->{$sub1} }; for (my $i=0; $i<$count; $i++)

    Easier to read:

    my $count = @{ $doc->{$sub1} }; for my $i ( 0 .. $count-1 )

    Alternative:

    my $last = $#{ $doc->{$sub1} }; for my $i ( 0 .. $last )

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://723642]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others having an uproarious good time at the Monastery: (5)
As of 2024-04-25 14:06 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found