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
In reply to Re: foreach loop
by davido
in thread foreach loop
by Anonymous Monk
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |