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

Hi all, I'm completely new to Perl. What I'd like to do is have a loop (I'm assuming using a counter) That will print the first line below,"div class="row-fluid hideInIE8 data" only once per 6 times that the foreach goes through. this is a .cgi page printing html. Right now I realize the above line is outside of the print statement. Any help would be greatly appreciated! Thank you in advance.

<div class="row-fluid hideInIE8 data"> CTHULHU foreach (@fs_list) { my ($up) = $_->{usepercent} =~ m/(\d+)\%/; print qq(\t\t<div class="span2" onTablet="span4" onDesktop="sp +an2">\n); print qq(<div class="circleStatsItemBox NSBlue">); print qq(<div class="header">$_->{fsname}</div>); print qq(<span class="percent">percent</span>); print qq(<div class="circleStat">); print qq(<input type="text" value="$up" class="widget"/>); print qq(</div>); print qq(<div class="footer">); print qq(<span class="count">); print qq(<span class="number">$_->{availsize}</span>); #print qq(<span class="unit">) . $_->{avilsize} =~ m/\d+(\w+)/ + . qq(</span>); print qq(</span>); print qq(<span class="divide"> / </span>); print qq(<span class="value">); print qq(<span class="number">$_->{totalsize}</span>); #print qq(<span class="unit">) . ($_->{totalsize} =~ m/\d+(\w+ +)/) . qq(</span>); print qq(</span>); print qq(</div>); print qq(</div>); print qq(</div>); } print << "CTHULHU";

Replies are listed 'Best First'.
Re: foreach loop
by InfiniteSilence (Curate) on May 14, 2014 at 13:49 UTC

    For every nth you can use modulus operator:

    perl -e 'for(1..100){ if($_%6==0){print qq|$_ sixth\n|;} };' 6 sixth 12 sixth 18 sixth 24 sixth 30 sixth 36 sixth 42 sixth 48 sixth 54 sixth 60 sixth 66 sixth 72 sixth 78 sixth 84 sixth 90 sixth 96 sixth

    Celebrate Intellectual Diversity

Re: foreach loop
by davido (Cardinal) on May 14, 2014 at 15:09 UTC

    In order to incorporate others' solutions into your code I have to ignore the nonsensical HERE doc usage. It doesn't make sense to print << "CTHULU"; at the end of your script, while the HERE doc's end tag appears as line 3.

    As several posts here suggested, increment a counter and use the modulus operator. Incorporating it into your code could nominally be as simple as this:

    my $modulator = 0; foreach (@fs_list) { print qq{<div class="row-fluid hideInIE8 data">\n} unless $modulator++ % 6; my ($up) = $_->{usepercent} =~ m/(\d+)\%/; print qq(\t\t<div class="span2" onTablet="span4" onDesktop="sp +an2">\n); print qq(<div class="circleStatsItemBox NSBlue">); print qq(<div class="header">$_->{fsname}</div>); print qq(<span class="percent">percent</span>); print qq(<div class="circleStat">); print qq(<input type="text" value="$up" class="widget"/>); print qq(</div>); # ...and so on... }

    This is an idiom that comes up frequently in programming when the goal is to do something every n-th step. And it's not unique to Perl. It turns out that this is a pretty "good idea". Higher Order Perl says this, regarding "Good Ideas":

    There aren’t too many ideas that are both good and simple. The few that we have are used everywhere.

    This is one of those good and simple ideas that gets used everywhere.


    Dave

Re: foreach loop
by vinoth.ree (Monsignor) on May 14, 2014 at 13:44 UTC

    Hi

    Use counter like this, here is psudocode

    my $count=0; foraech(@fs_list) { $count++; if($count == 6) { print qq(<div class="row-fluid hideInIE8 data">); $count=0; } ... ... ... }

    All is well
Re: foreach loop
by Anonymous Monk on May 14, 2014 at 14:03 UTC

    Thank you both for the quick responses! I'll give them a try and see if it works for my purposes.

Re: foreach loop
by Anonymous Monk on May 14, 2014 at 15:31 UTC

    Once again, thank you all for taking the time to respond so quickly, very much appreciated indeed. I have the code working as desired now. Dave, loved the quote btw! =)

Re: foreach loop
by Anonymous Monk on May 14, 2014 at 14:27 UTC

    thanks again guys.. After checking it out it seems the second way may work for what I want it to do but I'm not sure how to implement it into my existing code. To be clear, I need that first "div class="row-fluid.." to print the first time and every 6th time after that. Again, Thank you for your help

      for my $i (0..$#fs_list) { if ($i % 6 == 0) { ... div ... } ... $fs_list[$i] ... }
      or
      ... div ... for my $i (0..$#fs_list) { ... $fs_list[$i] ... if ($i % 6 == 0) { ... div ... } }
      depending on what you want

      Hi

      print once before the foreach loop then.


      All is well