in reply to Searching Files

Hi -- in the interest of helping you help yourself I would suggest you break up the problem into subproblems.

  1. Read in this month's file and use the split function to find the first word on each line, then update a hash, using that first word as the key.
  2. Determine the name of the file for each of the previous three months (sort of tricky -- think about in February 2003 -- you need to get 200211, 200212, and 200301).
  3. For each of those files:
    • create a new hash, let's call it the "counter" hash
    • read in each record and split to find the first word.
    • if the word is in the "this month" hash, increment your counter hash.
    • when you're done reading a given file, print out the counter hash.
Each of these subproblems is pretty simple on its own, so start there and report back if you're struggling.

Replies are listed 'Best First'.
Re: break up the problem
by rchou2 (Novice) on Jul 24, 2002 at 21:29 UTC
    cebrown, I tried doing this...... I already created a hash dataname with the names......
    foreach $Name (sort keys %dataname) { for (APRIL, MAY, JUNE) { if ($count{$Name}) { $count{$Name}++; } } printf OUTFILE "%-50s %d\n", $Name, $count{$Interface_Name};
    but it just seems to be adding 1 count after each loop...any suggestions?
      Are you sure your code is doing what you think it's doing? When I read it, it's saying
      1. For every name in this list, do the following:
        1. If I have a non-zero number in the location $count{$Name}, then I need to add 1 to the location specified by $count{Name}
      2. Print stuff out to somewhere.

      That doesn't seem like that's what you want to do. In fact, it's doing exactly what you're complaining it's doing. :-)

      ------
      We are the carpenters and bricklayers of the Information Age.

      Don't go borrowing trouble. For programmers, this means Worry only about what you need to implement.

      Well, "APRIL, MAY, JUNE" is just a list of three strings. Perl is iterating across each element in that list and doing the stuff inside the for loop exactly three times, once for each string.

      Elsewhere in the program I assume you did something like open APRIL, "<200204";. If that's the case, you need to use the special angle bracket operator to iterate through each record in a file, like while(<APRIL>) {do stuff}. Note also that while you are looping through each file you still need to perform the split on each record in order to find the name that's on a given record.

        Well, "APRIL, MAY, JUNE" is just a list of three strings.

        Hopefully not. Or to be more precise: Hopefully they're constants (which can be strings, but who knows?).

        Cheers,
        -Anomo