in reply to Re: Looping through a file
in thread Looping through a file

I have a print request from a database query I just ran. I'd like to check to see if I've already printed this request. Previo +us requests are in a .txt file. Open the text file and read in all the prior print jobs. If the print request from the database query is in the text file, skip + it. If the print request from the database query is not in the text file, +create a new .dat file for printing.

Replies are listed 'Best First'.
Re^3: Looping through a file
by apl (Monsignor) on Apr 16, 2008 at 01:07 UTC
    First, you want to load the contents of <PARTS> into a hash, not an array.

    Then, you test for the existence of the database query in the hash. If it's not already defined, create the new file.

    How would you implement these two pieces?

      I've created a hash like this:
      open(PARTS, $partlist) || die "Can't open file: $!"; chomp(@PARTS = <PARTS>); %hash_table = (@PARTS);
      To evaluate it so I do something like this?
      foreach (keys %hash_table) { if ($_ eq $ordnum) { #do something } else { #do something else } }
        A simpler way to test for existance (and the reason I love hashes) is to replace
        foreach (keys %hash_table) { if ($_ eq $ordnum) {
        with
        if ( defined( $hash_table{ $ordnum } ) ) {