$dirName = "/blahdeblah/source"; open (FIELDS, "fields.dat") or die $!; open (FOUND, ">found.dat") or die $!; while () { $field = $_; while (defined($nextfile = <$dirName/*cm*.4gl>)) { $rc = system ("grep $field $nextfile 1>/dev/null"); if (!$rc) { print FOUND "$field $nextfile\n"; last; } } }