Frederic_S has asked for the wisdom of the Perl Monks concerning the following question:

Hi there! I have the following construction in my program:
[% IF user.login == Coordinator1 || user.login == Coordinator2 %] <br> <Table class=table> <tr class="[% loop.parity %]"> <td class=title_td colspan=6>Coordinator overview:</td> </tr> <tr> <td class=title_td>Date</td> [% FOREACH AdminData IN AdminValues %] <td class=title_td>[% AdminData.RealName %]</td> [% i = loop.count %] [% n = i %] [% END %] </tr> [% FOREACH Time IN AdminTime %] <tr class="[% loop.parity %]"> <td class=title_td>[% Time.Day %]</td> [% FOREACH AdminDate IN AdminEndDate %] <td class=var_td>[% AdminDate.AppEnd %]</td> [% LAST IF loop.count == i %] [% i = i + n%] [% END %] </tr> [% END %] </Table> [% END %]
As you can see, there is some template-toolkit script inbetween. What I would like to achieve, is to break the third foreach loop after the first 5 iterations, then start again AFTER the first five for the next five iterations, then break again and start AFTER the first 10 iterations. Is that possible in Perl? I have no idea how else I could achieve the output I need. The problem is this - the values are read from a database table. This table lists users and their anticipated deadlines for a project. These deadlines are saved once a day for each user, in order to see over time, how a user rates himself. Say I have 5 users for a project. Every day, I save these 5 users to the database, together with todays date and 5 different deadlines. I only need todays date ONCE, but all 5 deadlines. That's the output I'm trying to get in the above code sample. I extract the values of the users into a hash and output these in the first foreach loop. Then I select the saved days and output them in the second foreach loop, creating a new row for each day. Then, I want to output the deadlines as columns in each row. I hope I was able to describe what I'm aiming to achieve - sorry for not being clearer, english isn't my mothertongue :-( Cheers Fred

Replies are listed 'Best First'.
Re: How to skip certain values in a foreach loop
by samarzone (Pilgrim) on Dec 28, 2010 at 12:21 UTC

    Your problem is not very clear to me but I am assuming that for each value of AdminTime you want to fetch five values from AdminEndDate.

    Is that possible in Perl?

    It is more of a logic issue rather than Perl issue

    You can pop out five values from AdminEndDate for each iteration of AdminTime. You can also take a counter in AdminTime's loop and use it to get the starting index of AdminEndDate.

    I hope that directs to you towards solution. I haven't worked on Template so I won't be able to show you how to code it.

    --
    Regards
    - Samar
      Hello Samar! Thank you - yes that's exactly what I want to do. With the current code, the output looks somewhat like this:
      date user1 user2 user3 user4 user5 day1 dl1 dl2 dl3 dl4 dl5 day2 dl1 dl2 dl3 dl4 dl5 dl6 dl7..dl10 day3 dl1 dl2 dl3 dl4 dl5 dl6 dl7..dl15
      This is what I want:
      date user1 user2 user3 user4 user5 day1 dl1 dl2 dl3 dl4 dl5 day2 dl6 dl7 dl8 dl9 dl10 day3 dl11 dl12 dl13 dl14 dl15 ....
      What would be the perl code to skip the first 5 iterations of the second loop and first 10 of the third loop? Adapting that code to the template is quite easy, so that wouldn't be the problem. Thank you for your help! :-) Regards Fred
        Maybe I should also elaborate where I get the data from. This is how the data is fetched and proccessed:
        # In the main script: my @ReturnValues; my @AdminValues; my @AdminTime; my @AdminEndDate; Admin(\@ReturnValues, \@AdminValues, \@AdminTime, \@AdminEndDate); # This is the code in sub Admin: my $sql = "SELECT DISTINCT user_id, realname FROM database.time_data W +HERE version='$Version' AND product_id='$ProductID' AND milestone='$M +ileStone'"; my $sth = $dbh->prepare($sql); $sth->execute(); while(my @RowAdmin = $sth->fetchrow_array()) { my %AdminData; $AdminData{"UserID"} = $RowAdmin[0]; $AdminData{"RealName"} = $RowAdmin[1]; # Insert them into hash push (@$AdminValues, \%AdminData); } # Get the stored days $sql = "SELECT DISTINCT day FROM database.time_data WHERE version='$Ve +rsion' AND product_id='$ProductID' AND milestone='$MileStone'"; $sth = $dbh->prepare($sql); $sth->execute(); while(my @RowTime = $sth->fetchrow_array()) { my %AdminTimeData; $AdminTimeData{"Day"} = $RowTime[0]; push (@$AdminTime, \%AdminTimeData); my $sql2 = "SELECT app_end FROM database.time_data WHERE day='$AdminTi +meData{'Day'}' AND version='$Version' AND product_id='$ProductID' AND + milestone='$MileStone'"; my $sth2 = $dbh->prepare($sql2); $sth2->execute(); while(my @RowTime2 = $sth2->fetchrow_array()) { my %AdminAppEnd; $AdminAppEnd{"AppEnd"} = $RowTime2[0]; push (@$AdminEndDate, \%AdminAppEnd); } } $sth->finish(); $dbh->disconnect(); # Back in the main script: # Pass Values to template $vars->{'ReturnValues'} = \@ReturnValues; $vars->{'AdminValues'} = \@AdminValues; $vars->{'AdminTime'} = \@AdminTime; $vars->{'AdminEndDate'} = \@AdminEndDate;
        How the data is then proccessed in the template can be seen in the first Post. I hope this helps a bit. Regards Fred