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

Hello, I'm tasked with taking an input.txt file that contains in various lines, the word 'new'. I need to create an output.txt file that switches all instances of 'new' regardless of case, to 'old'. I've opened my input.txt file, opened my output.txt file, and have a Regex to make the correct switches, but how do I get it to write to my output.txt file? Any help is appreciated!

use strict; use warnings; open(my $in, "<", "input.txt") or die "Cannot open < input.txt: $!"; open(my $out, ">", "output.txt") or die "Cannot open < output.txt: $!"; while(my $line = <$in>) { $line =~ s/new/old/i; }

Replies are listed 'Best First'.
Re: [Class Assignment] Regex and Output Files
by toolic (Bishop) on Feb 20, 2015 at 15:31 UTC
    • print to your filehandle: print $out $line;
    • perlre: case-insensitive uses s///i
    • Use warnings too

    UDPATE: Always mention that you cross-posted on another site (SO) and provide the URL.

      Thank you, still new here so I was not aware. So I've gone through and made the changes you've suggested. However, nothing happens with my output.txt file once the code is executed.
      use strict; use warnings; open(my $in, "<", "input.txt") or die "Cannot open < input.txt: $!"; open(my $out, ">", "output.txt") or die "Cannot open < output.txt: $!"; while(my $line = <$in>) { $line =~ s/new/old/i; print $out $line; }

        I used your most recent code with this input.txt file:

        Out with the old, in with the new. Old stuff is cool. New stuff is also cool. Now to mess with your head: Fortune favors the bold. I knew it.

        It produced the following output.txt file:

        Out with the old, in with the old. Old stuff is cool. old stuff is also cool. Now to mess with your head: Fortune favors the bold. I kold it.

        Do you see the problem(s) yet?

        Hint:It is a useful skill to be able to fabricate good test data.

        nothing happens with my output.txt file once the code is executed
        I don't know what that means. See also: http://sscce.org
Re: [Class Assignment] Regex and Output Files
by Athanasius (Archbishop) on Feb 20, 2015 at 16:07 UTC

    Hello Hayest,

    the word 'new'. ... create an output.txt file that switches all instances of 'new' regardless of case, to 'old'.

    Something to consider: Do you really want to replace every sequence of the characters ‘n’, ‘e’, ‘w’, or only those sequences which constitute the word ‘new’? For example, should ‘renewal’ be changed to ‘reoldal’ or left unchanged? If the latter, then you’ll need to use the regex assertion \b to match word boundaries. See “Assertions” in perlre, and the discussion of the word anchor in perlretut.

    Hope that helps,

    Athanasius <°(((><contra mundum Iustus alius egestas vitae, eros Piratica,

      Yes, don't make this clbuttic mistake!

        Wow, tough crowd for a Friday afternoon. From http://www.computerhope.com/jargon/c/clbuttic.htm:

        More commonly known as the Scunthorpe problem, clbuttic is an issue that occurs with filters such as spam filters that mistakenly blocks or denies something that is valid. The Scunthorpe problem derives from the swearword filter AOL was using in 1996 that blocked valid users who were living in this town from creating accounts from the obscene keyword found within this towns name.

        There's even a Wikipedia article.

        Telegraph article: http://www.telegraph.co.uk/news/newstopics/howaboutthat/2667634/The-Clbuttic-Mistake-When-obscenity-filters-go-wrong.html

        Blog with more references that also mentions \b: http://blog.codinghorror.com/obscenity-filters-bad-idea-or-incredibly-intercoursing-bad-idea/

Re: [Class Assignment] Regex and Output Files
by Anonymous Monk on Feb 20, 2015 at 15:43 UTC