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

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?

Replies are listed 'Best First'.
Re^4: Looping through a file
by baixiaohu (Initiate) on Apr 24, 2008 at 14:50 UTC
    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 } ) ) {
        Thanks apl! Still muddling through this. When I do a data dumper my hash looks like this:
        $VAR1 = { 'MIXORD077768' => 'MIXORD077769' };
        What's mixing me up is why the hash is mapping like that? The data in the partlist.txt file is:
        MIXORD077769 MIXORD077768