Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

Trying to display subcategories and counts

by Anonymous Monk
on May 04, 2000 at 22:05 UTC ( [id://10249]=perlquestion: print w/replies, xml ) Need Help??

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

I am a total newbie to perl, teaching myself so please bear that in mind!

I have a database (pipe delimited flat file) that I am accessing through CGI using a query_string eg. getoffers.cgi?Category&subcategory (eg. getoffers.cgi?Household&Alarms - this will give a list of all the alarm companies within the Household category)

The fields are in this order:
category|subcategory|company|etc|etc

I have this part working fine and displaying the offers from their respective category and sub-category within the database file.

The problem I have is that I want to be able to display the list of sub-categories and the number of offers within each in a given category by calling getoffers.cgi?Category (this would, in the case of Household list all the sub-categories like so: Alarms (3) Antiques (2) Sub-category and the number of entries with that sub-category in brackets)

I am really stuck on this, I guess I have to make an array of all the sub-categories and then count the amount of same named entries, then display only one of each.

I hope I explained this in a way that at least one of you can understand!

Thanks in advance for any help.
  • Comment on Trying to display subcategories and counts

Replies are listed 'Best First'.
RE: Trying to display subcategories and counts
by PipTigger (Hermit) on May 04, 2000 at 23:22 UTC
    Don't count same named occurrences from an array. Use the names as keys to a hash so that any duplicates just overwrite the key/value of the same name that had been encoutered earlier. If you store something useful in each value, then you might not want to just clobber the old. To append new values (in the same key), you can say: $stufhash{$namekey} .= $newvalue;If the hash doesn't have a key '$namekey' yet, it will create one with value '$newvalue' but if it's already there, it'll be appended to the old. If they're all pipe-delimited things that you want to preserve for later spliting, you might need to test if it has a value and append a pipe to the end of it's value before adding the $newvalue. That is:
    if ($stufhash{$namekey}) { $stufhash{$namekey} .= "|" . $newvalue; } else { $stufhash{$namekey} = $newvalue; }
    I hope this is helpful. TTFN & Shalom.

    -PipTigger
Re: Trying to display subcategories and counts
by turnstep (Parson) on May 04, 2000 at 22:29 UTC
    Try this:
    while(<FLATFILE>) { @stuff = split(/|/, $_); push(@cats, $stuff[0]); ## Do other stuff with @cats or @stuff, and then: $subcat{$stuff[1]}++; } ## This creates a hash (%subcat) in which the keys are the subcategori +es, ## and the values are the total of each one. ## So, to output a pretty list: for $x (sort keys %subcat) { print "$x ($subcat{$x})\n"; }

    This will display them in alphabetical order. From the looks of your data, this is probably desired.

Re: Trying to display subcategories and counts
by plaid (Chaplain) on May 04, 2000 at 23:22 UTC
    What you'll need to do is, as described above, create some type of data structure out of the file beforehand. What you might want to do is start with a chunk of code like
    my($category, $subcategory) = split(/&/, $ENV{'QUERY_STRING'}); if($subcategory ne "") { # Read in the file and look for the subcategory # specified, as you are already doing } else { # Read in the file and count the values in the # subcategory, as per turnstep's answer above. }
    This would get you the desired behavior of having a CGI that can handle with and without a subcategory.
Re: Trying to display subcategories and counts
by Maqs (Deacon) on May 05, 2000 at 14:53 UTC
    That is not a native perl solution, but it is rather short and graceful, IMHO :):
    @subcats = sort(`grep $Catname $file | cut -f 2 -d '|'`);
    --
    With best regards
    Maqs.
RE: Trying to display subcategories and counts
by Anonymous Monk on May 05, 2000 at 09:49 UTC
    Gday, I hope this helps a little: My unerstanding is this: as you go through the file, count each occurrence of the subcategory and when you finish, display numbers for each subcategory. My example assumes stdin (so you can check it out on your file before you put it in a cgi... while(<>) { ($categ, $subcat, $therest) = split(/|/, $_, 3); $subcatcount{$subcat}++; } foreach $subcategory (sort keys(%subcatcount)) { print "$subcategory contains $subcatcount{$subcategory}\n"; } ...should do what you want. Regards, Pat Heuvel pheuvel@optusnet.com.au
RE: Trying to display subcategories and counts
by Anonymous Monk on May 05, 2000 at 09:52 UTC
    Gday, I hope this helps a little: My unerstanding is this: as you go through the file, count each occurrence of the subcategory and when you finish, display numbers for each subcategory. My example assumes stdin (so you can check it out on your file before you put it in a cgi...
    while(<>) { ($categ, $subcat, $therest) = split(/|/, $_, 3); $subcatcount{$subcat}++; } foreach $subcategory (sort keys(%subcatcount)) { print "$subcategory contains $subcatcount{$subcategory}\n"; }
    ...should do what you want. Regards, Pat Heuvel pheuvel@optusnet.com.au

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://10249]
Approved by root
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others examining the Monastery: (8)
As of 2024-04-18 12:56 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found