Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Foreach print question 1

Status
Not open for further replies.

ability1

Programmer
Joined
Dec 24, 2002
Messages
23
Location
US
I'm trying to print the results from an array

Lets use this as an example

@p_categories = (info1,info2,info3,info4,info5)

foreach $p_category (@p_categories){
print &quot;<tr><td> $p_category </td></tr>&quot;;

}

Now it's easy to have rows <tr> to simply run to infinity, but how would you write, to put the first say 3 results on the first <tr> and once the results hit above 3 for that line to start a new <tr> bracket....if you catch my drift.

 
This does the trick:

@p_categories = (info1,info2,info3,info4,info5,info6,info7,info8,info9,info10);
$i=0;
foreach $p_category (@p_categories) {
if ($i==0) {
print &quot;<tr>&quot;;
}
if ($i >= 0 && $i <= 2) {
print &quot;<td>$p_category</td>&quot;;
}
if ($i == 2) {
print &quot;</tr>\n&quot;;
}
$i++;
if ($i == 3) {
$i = 0;
}
}
print &quot;</tr>\n&quot;
 
Works perfect! Awesome job thanks for the help it is much appreciated
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top