in reply to Search Script

What you're doing now is reading the entire DAFILE while you're on the first line of HintsFile. You would need to re-open DAFILE (or seek to the beginning) on each pass through the loop to search it again. Of course, that's not really recommended.

What you should probably do is read all of one file or the other (whichever is expected to be smaller) into an array, and then use a foreach loop to walk through that array each time you read a line from the (other) file.


Caution: Contents may have been coded under pressure.

Replies are listed 'Best First'.
Re^2: Search Script
by arebc (Initiate) on May 04, 2005 at 17:20 UTC
    Roy thank you for replying. The below script is what I think you were saying.
    print"\n"; print "Please enter file name and path:"; chomp($filename = <STDIN>); print "File Name: $filename"; print "\n"; open DAFILE, "$filename"; open (HintsFile, "Hints.txt") or die "Can't find file\n"; #$input = <DAFILE>; $hints = <HintsFile>; open(testfile,"Hints.txt"); $i=0; @daarray = (); while(<testfile>){ $daarray[$i] = $_; $i++; } close(testfile); for($j=0; $j<$i; $j++) { $hints = $daarray[$j]; ($first,$last) = split(/\ == /,$hints); if($first =~ m/$input/i) {print "Found: $last"; print "\n"; } } print "Search Complete\n"; close DAFILE; close HintsFile;
    Thank you very much for the advice! I'm still having some issues with the search, any thoughts? arebc
      You've got a lot of clutter in there. You are opening Hints.txt in two places, for example. You need to clearly understand what you want the computer to do, and in what order. Then code that. It might help for you to write out the steps in English (or other language of your choice) in comments first. Then under each step, write the code to perform that step. You might find that the code almost writes itself, then.

      You have a lot of pieces that are more or less what you will want to use, but you haven't got them put together right. In particular, it doesn't look like you're reading DAFILE any more. You need to have a read-loop for that, surrounding the for-$j loop, so that the j loop is executed for every line read from DAFILE.


      Caution: Contents may have been coded under pressure.