Howdy Monks!

I seem to be having some difficulty in chapter 9 of the Llama. To all those who have helped me out with the other chp 9 questions, a ton of thanks. To anyone kind enough to help out here, please understand that I am a novice struggling to ride the Llama, and I will likely not understand anything not already either a) explained in detail there, or b) lucidly explained here. Thanks again for anyone with an offering though.

Okay, now on to the meat and potatoes. This question relates to Question 3 of chapter 9. This question is based on promblem 2 which states :"Write a program that makes a modified copy of a text file. In the copy every string Fred (case insensitive) should be replaced with Larry. (So, "Manfred Mann" should become "ManLarry Mann"). The input filename should be given on the command line (don't ask the user), and the output file name should be the corresponding filename ending with .out.

Question 3 states: "Modify the previous program to chage every Fred to Wilma and every Wilma to Fred. Now input like fred&wilma should look like Wilma&Fred in the output".

here's my solution:

#!/usr/bin/perl ## Copyright (C) 2008 Tim Dunphy use strict; my $file = $ARGV[0]; if (!$file) { print "Hey! You didn't give me a file, sucka!\n"; } my $out = $file; $out =~ s/\.\w+?$/.out/; open IN, "<$file" or die "File did not open\n"; open OUT, ">$out" or die "File not available for write.\n"; while (<IN>) { $_ =~ s/fred/\n/i; $_ =~ s/wilma/Fred/i; $_ =~ s/\n/Wilma/; print OUT; }
I do not understand why the output behaves the way it does. Here's what a sample run did to my text file. Here's the input file:
fred wilma wilma fred wilmafred fredwilma wilma&fred fred&wilma


and here is the resulting output, which as you can see is clearly not to my expectations:
Wilma FredWilmaFredWilmaWilma FredWilma WilmaFred Fred&Wilma Wilma&Fred Wilma
What really annoys me, is that I had this problem solved already. But a later exercise completely clobbered this answer and now I can't remember how to fix it!!! :D Anyone who can help me answer this question will help save my sanity.

Thanks!

In reply to search and replace weird output by bluethundr

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.