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


First off, I just wanted to say thanks for helping get my little script going. Almost everything is working great!

I am having one little problem though. Some of the names of the users contain symbols, for instance using the square brackets, or !,),{,* and many others. I've found a problem where if a name such as
[ROT] THE MAN
is used to search for, when it searches the line strings, it will assume we are using ROT THE MAN to search with, which is not the case, and ends up not actually finding what we need. I don't know if there is a different type of variable to use, or a different search to use or what, but I'm totally lost as to what to try next. Here's the code thus far...

Oh yeah, see "Simple file editing problems" for the original problem I had.

print "\nEnter extension of files to look in (example: log or tx +t):\n\n"; $fileext = <STDIN>; $fileext = lc($fileext); print "\nConfirmed: $fileext\n\n"; chomp $fileext; print "\nEnter exact name of player to find and replace:\n\n"; $playertofind = <STDIN>; print "\nConfirmed: $playertofind\n\n"; chomp $playertofind; print "\nEnter exact name of player to replace $playertofind with:\n\n +"; $newplayer = <STDIN>; print "\nConfirmed: $newplayer\n\n"; chomp $newplayer; @filestocheck = <*.$fileext>; foreach $file (@filestocheck) { open FILE, "$file" or die "\nError: Unable to open file for read. +.."; @lines = <FILE>; close FILE; open FILE, ">$file" or die "\nError: Unable to open file for writ +e..."; foreach $line (@lines) { $line =~ s/$playertofind/$newplayer/g; print FILE $line; } print "\nFile $file has been corrected.\n"; close FILE; }

To put it simply, I need to search for names that have square brackets or other symbols in them, and as of now, I don't think I can.
Any Ideas?

Thanks for all the help!! I know I still have a lot of work to do, but I'm still trying to get the functional part going first.

I really appreciate it!

Alan

Replies are listed 'Best First'.
Re: Simple file editing problems2
by sauoq (Abbot) on Oct 23, 2002 at 22:22 UTC

    You need to escape all the regex metacharacters. See perldoc -f quotemeta and read about \Q in perlre. One way to do it:

    $line =~ s/\Q$playertofind\E/$newplayer/g;
    -sauoq
    "My two cents aren't worth a dime.";
    
Re: Simple file editing problems2
by Ovid (Cardinal) on Oct 23, 2002 at 22:40 UTC

    As a side note, when you see duplicated code, it's time to refactor it. For example, you are prompting for, and reading, the user's responses, but you do this several times in a row. It looks like it's time for a subroutine.

    my $fileext = prompt("\nEnter extension of files to look in (example: log or t +xt):\n"); my $playertofind = prompt("\nEnter exact name of player to find and replace:\n"); my $newplayer = prompt("\nEnter exact name of player to replace $playertofind wi +th:\n"); sub prompt { my $message = shift; print $message; chomp ( my $response = <STDIN> ); print "\nConfirmed: $response\n\n"; return $response; }

    Remember: no duplicate code :)

    Cheers,
    Ovid

    Join the Perlmonks Setiathome Group or just click on the the link and check out our stats.

Re: Simple file editing problems2
by rir (Vicar) on Oct 23, 2002 at 22:56 UTC
    You may want to consider the substring problem, say you have these players: Jel, Jelo, Jello, Ros, Roseanne, Rosie, Rose. Will Roseanne be slighted when you change Ros to Roger?