texuser74 has asked for the wisdom of the Perl Monks concerning the following question:

i want to do some find and replace job in a text file. While the find text and replace with text are from a .mdb file. The .mdb file contains a table with 2 column, 1st column is full(full name) and the second one is abb(abbreviated name). In the text file i want to find the full text and replace it with the corresponding abbreviated text.
test.txt This is an example section. And the second line has structural science. jabb.mdb full | abb ------------- example | eg expand | exp section | sect science | sci test.out (required output) This is an eg sect. And the second line has structural sci.
With the following code i have written, nothing is happening.
use Win32::ODBC; open( IN, '<', 'test.txt' ) or die "Couldn't open infile.\n$!"; open( OUT, '>', 'test.out' ) or die "Couldn't open outfile.\n$!"; $dsn = "test_dsn"; $db = new Win32::ODBC($dsn); die "ERROR: Failed to open database\n" if(!$db); $sql = "SELECT * from jabb"; $db->Sql($sql); while ($db->FetchRow()) { ($full, $abb) = $db->Data("full", "abb"); while(IN){ s!$full!$abb!g; print $_; print OUT $_; } close(OUT); } $db->Close();
please help me in looping through the files.

Replies are listed 'Best First'.
Re: working with files
by davido (Cardinal) on Nov 02, 2005 at 08:54 UTC

    Without checking the return values of your open statements, can you be sure the files are actually being opened?

    open( IN, '<', 'test.txt' ) or die "Couldn't open infile.\n$!"; open( OUT, '>', 'test.out' ) or die "Couldn't open outfile.\n$!";

    You can also check to see that you're actually getting the results you want after the substitution by adding a print statement (for debugging purposes only) as follows:

    # after your substitution... print $_; print OUT $_;

    Dave

      Dave, thanks for your reply
      print $_;
      is not printing anything in the screen. the problem with my script is, program hangs as if an endless loop, no output to the screen or output file.

        This is a problem:

        while(IN){

        It should be

        while( <IN> ) {

        If you had been using warnings you would have seen this message:

        Bareword found in conditional at yourscript.pl line xx. Name "main::IN" used only once: possible typo at yourscript.pl line xx +.

        And if you had been using strict, you would have seen this fatal message:

        Bareword "IN" not allowed while "strict subs" in use at yourscript.pl +line xx. Execution of yourscript.pl aborted due to compilation errors.

        It will really help you to keep on top of these hard to spot bugs if you remember to start every script off with:

        use strict; use warnings;

        Update: I believe someone already mentioned to you in an earlier thread that you should be using strictures and warnings. Strictures and warnings will cause Perl to complain when it encounters things that are probably bugs in your code (or at least bad practices). And all that noise is helpful in isolating and eliminating such bugs and bad practices.


        Dave