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 SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

hash output inconsistant

Status
Not open for further replies.

ibjdt

Programmer
Nov 25, 2002
63
0
0
thanks in advance for helping.


i have a DB with columns 0-10.
i also have the following:

Code:
@1    = ("C", "D", "E"); #possible subset of hash item 1
@2    = ("F", "G", "H"); #possible subset of hash item 2
%type = ( 1=>"A", 2=>"B");

i want to look at each item in the hash and compare to DB column 7. if match, update a counter specific to that hash item, then look at each item in the corresponding subtype array versus column 8. if match, update a counter. when both hash items have been compared to each line in the DB - print findings.

i have the code below, and although i get good results for the hash items, i get bad data for the subtypes.

Code:
foreach $key (keys %type)
          {
               foreach $line(@DB)
               {
                    chomp ($line);
                    @fields = split(/::/, $line);
               
                    if (($fields[5] eq "TBA") && ($fields[7] eq "$type{$key}"))
                    {
                         $count[$key]++;
                         foreach $subtype(@$key)
                         {
                              if ($fields[8] eq "$subtype")
                              {
                                   $count[$subtype]++;
                                   last;
                              }
                         }
                    }
               }
          }
          foreach $key (keys %type)
          {
               $info{'RESPONSES'} .= "<li>$type{$key} ($count[$key])</li>\n" if ($count[$key]);
               foreach $subtype(@$key)
               {
                    if ($count[$subtype])
                    {
                         $info{'RESPONSES'} .= "<li>$subtype ($count[$subtype])</li>\n";
                    }
               }
          }

a weird problem also - when i remove the part of the code that looks at the subtypes

Code:
foreach $subtype(@$key)
                         {
                              if ($fields[8] eq "$subtype")
                              {
                                   $count[$subtype]++;
                                   last;
                              }
                         }

it shows zero results?? if i delete only the part that prints out the subtype results, it works fine.

thanks for any suggestions.
 
Okay, how your code looks to me...

your first line, "foreach $key (keys %type)"... according to the first little snippit of what your data looks like, $key would be 1 on the first loop and 2 on the second, where $type{$key} is A and then B.

Later in your code, you're using @$key, which is treating $key as if it were an array reference... which doesn't appear to be the case.

Example of an array ref:
Code:
my $key = [
   'red',
   'green',
   'blue',
   'yellow',
];
foreach my $color (@$key) {
   print "color: $color\n";
}

# or the $key could be declared like this
my @colors = ('red','green','blue','yellow');
my $key = \@colors;

That may be one of your potential problems in the code.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top