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

Hello, I am working on a cgi program that lists multiple flat, text db from which you can add/remove/view information. I'm trying to figure out how to add a "count" of how many line entries are in each category file. (ie. Cat: Biology Books - 11 entries) Example of the categories list looks like this:
art.gif=ArtsDance=The Arts - Dance $index=0; foreach $cat (@catagories) { @line_pair = split(/=/,$cat); $catagorypicture = $line_pair[0]; $catagorylink = $line_pair[1]; $catagorydescription= $line_pair[2]; print "<a href=\"$cgidir/classifieds.cgi?catagory=$catagorylin +k\" ><img src=\"$htmldir/$catagorypicture\" border=0 align=center> <B +>$catagorydescription</B></a><br>"; $index++; if ($index==3) { $index=0;
And the individual categories that I would like a "count" for look like this:
open(DATAFILEIN,"$catagory.dat") || print "This section is + currently empty...Please check back often!"; print "<table border=1 width=\"95%\">"; while (<DATAFILEIN>) { chomp $_; @line_pair = split(/=/,$_); $time = $line_pair[0]; $name = $line_pair[1]; $email= $line_pair[2]; $phone= $line_pair[3]; $subject= $line_pair[4]; $data= $line_pair[5]; $time2 = $line_pair[6]; $data = &stripBadHtml($data); $password= $line_pair[8]; $pictureurl= $line_pair[7]; $priceline= $line_pair[9];
Any help would be greatly appreciated...Lis

Replies are listed 'Best First'.
Re: Adding an entry count for each DB
by newrisedesigns (Curate) on Oct 16, 2002 at 00:19 UTC

    If it's all in one array, just print the number of items in the array out. $#catagories, for your purposes.

    In the second part of your code, why do you split everything into an array, then copy each list item to a scalar? Why not just leave them in the array, or split them into variables?

    ($time, $name, $email, $phone, $subject, $data, $time2, $pictureurl, $ +password, $priceline) = split(/=/, $_);

    Hope this helps.

    John J Reiser
    newrisedesigns.com

Re: Adding an entry count for each DB
by blokhead (Monsignor) on Oct 16, 2002 at 00:59 UTC
    I would suggest using some sort of nested structure to organize your data. Read all your flatfiles into data structures, then manipulate them in another part of the code. Perhaps even translate them into a structure that you can pass to HTML::Template (look into this, it will make your life easier). Something like this (untested):
    sub get_items { my $category = shift; open(DATAFILE,"$catagory.dat") or return undef; my @fields = qw/time name email phone subject data passwd picture pr +iceline/; my @items = (); while (<DATAFILEIN>) { chomp; my %hash = (); @hash{@fields} = split /=/, $_; push @items, \%hash; } return @items; }
    Then later in the code you can refer to each element like this:
    print $items[$i]{time}; print "There are " . scalar(@items) . " items\n"; # etc..
    In this case, the structure is already suitable for HTML::Template. But this way accessing the data is highly logical and much more reusable, with the named attributes, instead of putting everything in a variable name in your while loop. Maintaining the code through changes in your flatfile format will be easier.

    blokhead