Johnson, Reginald (GTI) wrote:
> Thanks for the assistance. This is what I ended up doing. The hash is
> much faster and I am getting what I require in my two output files. My
> mhs file has over 26 columns and is over 27,000 lines.
> 
> #!/usr/bin/perl

Always

  use strict;
  use warnings;

and a lot of simple problems will vanish on their own.

>          $file="/adsm/CRONJOBS/MHS/nodes_cleanedup";
>          $mhs="/adsm/CRONJOBS/MHS/mh_alloc.csv";
>          $file_out="/adsm/CRONJOBS/MHS/in_mhs";
>          $file_not="/adsm/CRONJOBS/MHS/not_found";

All of those variables should be declared with 'my', or even better you
could

  use constant FILE => '/adsm/CRONJOBS/MHS/nodes_cleanedup';
  use constant MHS  => '/adsm/CRONJOBS/MHS/mh_alloc.csv';

etc.

Also, it would be nice to call it something better than 'file'. Wouldn't
'nodes' be more informative?

>         open (INFILE, "<", "$file") or
>                         die "$file could not be opened: $!";
>         open (MHSFILE, "<", "$mhs") or
>                         die "$mhs could not be opened: $!";
>         open (OUTFILE, ">", "$file_out") or
>                         die "$file_out could not be opened: $!";
>         open (OUTFILE2, ">", "$file_not") or
>                         die "$file_not could not be opened: $!";

Lexical file handles are nicer:

  open my $infh, '<', FILE or die FILE, " could not be opened: $!";

etc.

and your should open them further down, just before you need to use
them.

>                 %mhsHash = ();

Just

  my %mhsHash;

is fine here. New hashes and arrays are always empty and new scalars and
undefined.

>         while (<MHSFILE>) {
>                 chomp($_);
>             ($sa,$alloc,$host,$alltherest) = split(/\,/,$_);
>                 $host=uc($host);
>               $mhsHash{$host} = $_;
>                 }

  open my $mhsfh, '<', MHS or die MHS, " could not be opened: $!";

  while (<$mhsfh>) {
    chomp;
    my (undef, undef, $host) = split /,/;
    $mhsHash{$host} = $_;
  }

  close $mhsfh;

>         while (  $line= <INFILE>) {
>                 chomp($line);
>                 if (exists $mhsHash{$line} ) {
>                         print OUTFILE "$mhsHash{$line}\n";
>                         } #end if
>                 else { print OUTFILE2 "$line\n"; }
> 
>         } #end while

I think your $line should be called $host, given the preceding code, so

  open my $infh, '<', FILE or die FILE, " could not be opened: $!";
  open my $outfh, '<', FILE_OUT
      or die FILE_OUT, " could not be opened: $!";
  open my $outfhnot, '<', FILE_NOT
      or die FILE_NOT, " could not be opened: $!";

  while (my $host = <$infh>) {
    chomp $host;
    if (exists $mhsHash{$host}) {
      print $outfh $mhsHash{$host}, "\n";
    }
    else {
      print $outfhnot $host, "\n";
    }
  }

> close(INFILE);
> close(MHSFILE);
> close(OUTFILE);
> close(OUTFILE2);

These closes are unnecessary if the are always going to be at the end of
the program. The MHS file should have been closed as soon as you were
finished reading it.

HTH,

Rob

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/


Reply via email to