Kenneth Jideofor [ MTN - Ikoyi ] wrote:

> 
> Hi Guys,
> 
> I have two files, nut.dat and data.dat.
> The first file, nut.dat, contains lines of eleven digit figures; each
> line in this file is an eleven-digit figure.
> The second file, data.dat, contains lines of spaced figures; each line
> having four groups of eleven-digit figures.
> In other words, each line in the file, nut.dat, has one field, while
> each line in the file, data.dat, has four fields.
> 
> I want to compare each field in the nut.dat file against the first field
> of each line in the data.dat file. Where the field in the nut.dat file
> matches the first field in any of the lines in the data.dat file, the
> entire line in the data.dat file is saved to a file, box.dat.
> 
> For example, the content of each file can be viewed as follows:
> 
> nut.dat file:
> 12345678912
> 56789876543
> 23456789652
> 34567123456
> 
> data.dat file:
> 12345678912  23456098734  12348907678  67342519806
> 23456789652  87456321452  45231987564  23675843902
> 34567123456  23456709819  25361728980  49872653418
> 
> 
> I wrote the following Perl script to perform the above task.
> The script works very fast for a small data.dat file while it is
> extremely very slow with a very huge data.dat file.
> I need to make the script faster.
> 
> Could you, please, assist me with an improved version of the script?
> 
> Regards,
> Ken
> 
> ____________ My Perl Script______________________
> 
> #!/bin/perl -w
> open (FILE1, "/opt/MISC/AUDIT/nut.dat");
> open (FILE2, "/opt/MISC/AUDIT/data.dat");
> open (OUT, ">>/opt/MISC/AUDIT/box.txt");
> @nuts = <FILE1>;
> @database = <FILE2>;
> close(FILE1);
> close(FILE2);
> 
> foreach $nut(@nuts) {
>         chomp($nut);
> foreach $database(@database) {
>         chomp(@database);
> $data = substr($database,0,11);
>         if ($data eq $nut) {
>                 print OUT "$database\n";
>                 }      
>         }
> }
> close(OUT);

My version:

use strict;

my @nuts;
open FILE1, "/opt/MISC/AUDIT/nut.dat" or die "open nut file: $!";
while (<FILE1>) {
        s/^\s+|\s+$//g;
        next if /^#/ || /^$/;
        push @nuts, $_;
}
close FILE1;

open OUT, ">>/opt/MISC/AUDIT/box.txt" or die "open output file: $!";
open FILE2, "data.txt" or die "open data file: $!";
while (<FILE2>) {

        my $data = substr $_, 0, 11;
        foreach my $nut (@nuts) {
                print OUT $_ if $data eq $nut;
        }
#       print OUT $_ if grep /$data/, @nuts;    # this may be faster than foreach above
}
close FILE2;
close OUT;

__END__


-- 
  ,-/-  __      _  _         $Bill Luebkert    Mailto:[EMAIL PROTECTED]
 (_/   /  )    // //       DBE Collectibles    Mailto:[EMAIL PROTECTED]
  / ) /--<  o // //      Castle of Medieval Myth & Magic http://www.todbe.com/
-/-' /___/_<_</_</_    http://dbecoll.tripod.com/ (My Perl/Lakers stuff)

_______________________________________________
ActivePerl mailing list
[EMAIL PROTECTED]
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs

Reply via email to