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

Hello Everyone, I am in need of a little help on a project that I am working on. Here's what I'm trying to accomplish: I am using a program that searches a flat-db and shows each search match in a new table row. I really need it to list out "Search Matches" in a table that would be 3 columns across and 4 rows down. Any suggestions on how to accomplish this or any online examples anyone knows of? Here's the snip of script:
foreach $line (@array){ if ($line =~ /\Q$search\E/i){ ($category,$item_name,$item_price,$item_picture)=split(/\|/,$line); print <<End_of_line; <tr> <td valign=top CLASS=MENU1> <img src="$photo_loc/$photo1" width=75 height=75><BR> <B>$item_name</B><BR> $item_price<BR> </td> </TR> End_of_line } }
Any help, or suggestions would be greatly appreciated! Lis

Replies are listed 'Best First'.
Re: Database Listing Question
by Roger (Parson) on Jan 22, 2004 at 01:37 UTC
    You could build an array of HTML table cell fragments, and then iterate through 12 elements of the array. Each row will begin at index % 3 == 0, and end at index % 3 == 2.
    # --- push everything into an array --- my @match = (); foreach my $line (@array) { if ($line =~ /\Q$search\E/i) { my ($category,$item_name,$item_price,$item_picture) = split /\|/, $line; push @match, <<End_of_line; <td valign=top CLASS=MENU1> <img src="$photo_loc/$photo1" width=75 height=75><BR> <B>$item_name</B><BR> $item_price<BR> </td> End_of_line } } # --- Print the table --- my $inrow; print "<table>\n"; for (0..11) { # want 12 images $cell = shift(@match) or last; $inrow++, print "<tr>" if ($_ % 3) == 0; print "$cell"; $inrow--, print "</tr>\n" if ($_ % 3) == 2; } print "</tr>\n" if $inrow; print "</table>\n";

      Hi Roger, That did the trick. Thank you so much! I sincerely appreciate your assistance. Lis
Re: Database Listing Question
by ysth (Canon) on Jan 22, 2004 at 01:44 UTC
    untested:
    my @matches = grep /\Q$search\E/i, @array; if (@matches > 12) { print @matches-12, " matches ignored<br />\n"; $#matches = 11; } while (my @row = splice(@matches, 0, 3)) { print "<tr>\n"; for my $item (@row) { my ($category,$item_name,$item_price,$item_picture)=split(/\|/,$ +item); print <<End_of_line; <td valign=top CLASS=MENU1> <img src="$photo_loc/$photo1" width=75 height=75><BR> <B>$item_name</B><BR> $item_price<BR> </td> End_of_line } print "</tr>\n"; }
Re: Database Listing Question
by Abigail-II (Bishop) on Jan 22, 2004 at 01:18 UTC
    What exactly is your question? What's wrong with the code presented?

    Abigail

      Hi Abigail, Currently the script will display each database item in a new table row. I'm trying to figure out how to list the items three(3) across as shown:
      ITEM1 ITEM2 ITEM3 ITEM4 ITEM5 ITEM6
      Thanks!
        Your code doesn't make a row for each item. It creates a row for each row returned. However, in that row you have just one cell, and you separate the items with line breaks, which seems to confuse you in thinking that there's a row for each item.

        If you don't want the line breaks, then don't put them there! If you want columns, create them!

        Abigail