in reply to Nested For Loop giving me extra iterations

The problem is that in the third part of your for loop you are incrementing $i, and in your main loop you are assigning what you want it to be.

For the record I would never think of using a double-loop to solve this problem. Instead I might build a hash of arrays like this (untested) code:

my %group2user; foreach my $rec (@all_groups) { push @{$group2user{$rec->[0]}}, $rec; } foreach my $group_id (sort keys %group2user) { # Do whatever }
or else I would assume that the groups appeared grouped and work like this:
my $old_id; for my $rec (@all_groups) { if (not defined $old_id or $old_id <=> $rec->[0]) { # Spit out header $old_id = $rec->[0]; } # Print user record }
With either approach I push work down to Perl, and wind up with more reliable code. :-)

Replies are listed 'Best First'.
Re: Re (tilly) 1: Nested For Loop giving me extra iterations
by jreades (Friar) on Dec 11, 2000 at 20:37 UTC

    After a good night's sleep and some thought I did come up with a way to do what I wanted using a single for loop. The reason that I had to keep working on it, depsite tilly's code is that the output wasn't quite what I needed...

    To define the issue a little better -- I am outputting tables, and for the sake of rendering speed, it would be faster to output only a single row for each group with a single cell containing all the users who belong to that group. tilly's second snippet, while clean, can only (at least, as far as I can figure it) easily output a row per user... although I could simply output the group name once by testing against the old_id to see when I'd moved into a new group.

    Here's what the output should look like in pseudo HTML:

    <table> <row> <cell>Group 1 name <cell>Email 1, Email 2, Email 3, Email 4... <row> <cell>Group 2 name <cell>Email 5, Email 6, Email 7... </table>
    <cell>

    The reason I wanted to avoid the hash-based solution was that my data is already ordered by the SQL query I'm doing, so it seems strange to stuff ordered information into an 'unordered' hash only to have to sort it again.

    So, here's what I finally came up with:

    print <<EOTableHeaders; <table border="1" cellpadding="3" cellspacing="4"> <tr> <td class="list"><b class="list">Group Name</b></td> <td class="list"><b class="list">Group Members</b></td> <td class="list"><b class="list">Edit Mail Group</b></td> </tr> EOTableHeaders for (my $i = 0, my $j = 0; $i < (scalar(@{$all_groups}) - 1); $i++, $j +++) { print "<tr"; if ($j % 2 == 0) { print qq|bgcolor="#AAAACC"|; } print ">\n"; print qq|<td valign="top"><span class="list">| . $all_groups->[$i] +[1] |</span></td>\n|; print qq|<td valign="top"><span class="list">|; while ((defined($all_groups->[($i + 1)][0])) && ($all_groups-> +[$i][0] == $all_groups->[($i + 1)][0])) { print $all_groups->[$i][2] . "<br>"; $i++; } print $all_groups->[$i][2]; print "</td>\n"; print qq|<td><a href="edit_group.html?group_id=| . $all_groups->[$ +i][0] . |&&group_name=| . Apache::Util::escape_uri($all_groups->[$i][ +1]) |>Edit this Email Group</a></td>\n|; print "</tr>"; } </tr> </table>

    Or, for a slightly cleaner version:

    for (my $i = 0; $i < (scalar(@{$all_groups}) - 1); $i++) { print "Group name: " . $all_groups->[$i][1] . "\n"; while ((defined($all_groups->[($i + 1)][0])) && ($all_groups->[$i] +[0] == $all_groups->[($i + 1)][0])) { print "\tEmail: " . $all_groups->[$i][2] . "\n"; $i++; } print "\tEmail: " $all_groups->[$i][2] . "\n"; }