How many Items in each Category

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • virtualweb
    New Member
    • Aug 2007
    • 30

    How many Items in each Category

    Hello I'm doing a basic Item Catalog based on Flat File databases.

    My Category Database (Categories_Lis t.txt) has Categories in this manner:

    OILS
    CANNED FOODS
    LIQUORS
    WINES
    VEGETABLES

    My Product Database (Items_List.txt ) has Items and Categories in this manner:

    OILS|Corn Oil
    CANNED FOODS|Tuna
    CANNED FOODS|Sardines
    LIQUORS|Whiskey
    LIQUORS|Vodka
    LIQUORS|Rum
    WINES|Red Wine
    WINES|Rose Wine
    WINES|White Wine
    WINES|Sparkling
    VEGETABLES|Spin ach
    VEGETABLES|Cucu mber
    VEGETABLES|Char d
    VEGETABLES|Carr ot
    VEGETABLES|Onio n

    What I want my code to do is print to the browser the following list of Categories with the number of items in each category in this manner:

    OILS = 1
    CANNED FOODS = 2
    LIQUORS = 3
    WINES = 4
    VEGETABLES = 5

    The wrong results of my code are at:

    http://clubsostenible. de/cgi-bin/priya/Item_Quatities/List_Item_Quant ities.pl

    If you can suggest how to fix this here is my code..:

    Code:
    #!/usr/bin/perl -W
    print "Content-type: text/html\n\n";
    use CGI qw(:standard);
    use CGI::Carp (fatalsToBrowser);
    $query = new CGI;
    
          $Category_File = "Categories_List.txt";
    
    
           open (CATS, "<$Category_File") || die print"Error 89: Cant open<br>($Category_File)<br>$!<hr>";
           @all_cats = <CATS>;
           close (CATS);
           
           $Cat_Qty = @all_cats;
           
           for($i = 0; $i <= $Cat_Qty; $i++){
           	chomp($all_cats[$i]);
           	$Category = $all_cats[$i];
           	
           	$All_Items_File = 'Items_List.txt';
           	
           	                open (ITEMS, "<$All_Items_File") || die print"Error 89: Cant open<br>($All_Items_File)<br>$!<hr>";
                                       @all_items = <ITEMS>;
                                       close (ITEMS);
                                       
                                                $e = '0';
                                                foreach $item_specs (@all_items){
                                                $e++;
                                              
                                                ($Saved_Category,$Item_Name) = split(/\|/, $item_specs);	
                                                chomp($Saved_Category,$Item_Name);
                                                      
                                                        if($Saved_Category eq "$Category"){
                                                        $Item_Qty[$i] = "$e";
                                                        }
                                                    
                                                }               
      print"<center>$Category = $Item_Qty[$i]<hr>";        
           }
    Thanx for your help
    All files are attached here
    virtualweb
    Attached Files
  • RonB
    Recognized Expert Contributor
    • Jun 2009
    • 589

    #2
    Your script has a number of problems.

    First, it's missing 2 very important pragmas which help to point out common mistakes and should be in EVERY perl script you write. Add these 2 lines.
    Code:
    use strict;
    use warnings;
    The strict pragma will require you to declare your vars, which is done with the 'my' keyword.
    e.g.,
    Code:
    [B]my[/B] $Category_File = "Categories_List.txt";
    When opening a filehandle, you should
    1. use a lexical var for the handle instead of a bareword
    2. use the 3 arg form of open
    3. do not use die and print in the same statement - just use die

    e.g.,
    Code:
    open my $cat_fh, '<', $Category_File
      or die "Error 89: Cant open<br>($Category_File)<br>$!<hr>";
    You're using the wrong data structure. Instead of slurping the category file into an array, you should load it into a hash where the categories are the keys and initialize each of the values as a reference to an empty array. Using a hash will make it very easy to track both the individual items as well as the count of items in each category.

    You are opening and looping over the entire All_Items_File for each item in the category file. That's very inefficient. You should only loop over that file once and load the item into the corresponding hash element.

    Once all of the data has been loaded into the hash, you would then sort and output the data.
    Last edited by RonB; Sep 20 '13, 06:37 PM.

    Comment

    Working...