Hi again, since I didn't know how to edit my top post and didn't want to start a new thread, I will update here. Well part of my program works. The replacement string works but it doesn't preserve the capitalizations from the original text. For example: This is what it currently does:
User Input: She sells Seashells by the seashore. Search Pattern: sea Replacement Pattern: bay Final Output: She sells bayshells by the bayshore. But what I want it to do is keep the capitalization. Final Output: She sells Bayshells by the bayshore.
I don't really know where to begin on this. All I know is that I am not suppose to use arrays or hashes and I'm suppose to convert all the User Input into a lower case copy and work from there. I know some of the functions I am allowed to use are index, substr, length, rindex, lc, lcfirst. Since I can't really expect answers without trying, here's the algorithim I thought up but don't know how to implement. Let's say User input is stored in $input Then I make a lower case copy of $input and call it $lcinput When the user enters the search pattern $pattern and we make it also all lower case. So the program searches for $pattern in $lcinput, when it finds it, index should get the position of the pattern in $lcinput and compare it to that position on $input. If the strings are equal then it just inserts the replacement pattern $replace into both positions in $lcinput and $input to keep the string lengths the same. If the string compare returns that at a certain position the words are unequal then we know there's a capitalization in $input and we modify $replacement to be capitalize. After it goes through the whole length of $lcinput, then we know that we've indirectly gone through $input also. Thus we are done and we print $input and that should be the correct output. I'm sure there is some faulty thinking in there somewhere. But if anyone can point me towards a direction or where to look so i can reference without hashes, arrays, slices, list, and so forth let me know. Here's what I have:
main(); sub main { print "Text to be analyzed.\n"; print "Type a blank line when finished.\n\n"; while (<>) { /^$/&& last; $input.=$_} chomp($input); replace (); $input = ""; again (); } sub replace { print "\nSearched for: "; $pattern = <>; chomp($pattern); print "\nReplace with: "; $replacement = <>; chomp($replacement); $input =~ s/$pattern/$replacement/ig; }

In reply to Re: Searching and Replacing by Satira
in thread Searching and Replacing by Satira

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.