Hi, I'm really new at Perl. I'm one day into the language to be exact. :) Already, I'm stuck.
I'm trying to write a multipart program in Perl. The first part searches the user's input for a certain pattern (which the user specifies the pattern) and replace it with another pattern (which the user also specifies) and prints it out. I don't know Perl well enough to know what I'm doing wrong - which is probably everything. Here is what I have.
sub replace{
print "\nSearch Pattern: ";
$pattern = <STDIN>;
chomp($pattern);
print "\nReplacement Pattern: ";
$replacement = <STDIN>;
chomp($replacement);
while (<>){
if ($pattern){
substr($_, 0) = "$replacement";
}
}
}
Another thing I'm trying to do is make sure that if the pattern is capitalized in the search file (not the search pattern), my replacement pattern should also mirror it. I don't know if knowing this will change my programming or not. I hope that wasn't too confusing. Any help is much appreciated. (If you don't mind, please comment so I can learn.)
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: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.