CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Regular Expressions:
Strange behavior of variable in substitution expression

 



Fred
User

Aug 20, 2012, 3:34 AM

Post #1 of 9 (7502 views)
Strange behavior of variable in substitution expression Can't Post

Hello,

I am working on a simple search/replace program to replace some characters by another ones in a file.

The program ask the user the substitution he wants to make.

I would like to replace a newline by a tabulation with this program but I never succeeded Crazy

If the user enters with the keyboard <\n> as a search character and then <\t> as a replacement character, the newline \n is well recognized by the program, but the tabulation \t is not. As a result, the program replace each newline by the character <\t>.

If in my program, I set :
$with = "\t" ;
Then the program works perfectly, but I did not found how to enter \t with the keyboard (I tried "\t", '\t', \t ...), nothing work...

Many thanks in advance for your help !


Code
 
# Simple search / replace program

print "Replace: " ;
my $replace = (<>) ;
chomp $replace ;
$replace = qr/$replace/ ; # use $replace as a regexp


print "with: " ;
my $with = (<>);
chomp $with ;


open(FILEIN, $file_in) ;
open(FILEOUT, "> $file_out") ;

while (<FILEIN>) {
s/$replace/$with/g ;
print FILEOUT "$_" ;
}


BillKSmith
Veteran

Aug 20, 2012, 4:57 AM

Post #2 of 9 (7497 views)
Re: [Fred] Strange behavior of variable in substitution expression [In reply to] Can't Post

On windows, I can just type the Tab and Enter keys. If you want to use the escape sequence, you could test for it and convert it in your program.
Good Luck,
Bill


Fred
User

Aug 20, 2012, 5:12 AM

Post #3 of 9 (7495 views)
Re: [BillKSmith] Strange behavior of variable in substitution expression [In reply to] Can't Post

Thank you Bill !

But now, imagine that I want to replace any space by a newline... I have to find another solution Tongue


Laurent_R
Veteran / Moderator

Aug 20, 2012, 5:32 AM

Post #4 of 9 (7493 views)
Re: [Fred] Strange behavior of variable in substitution expression [In reply to] Can't Post

Hi,

this is not the most elegant piece of code, and there is probably a better solution, but the following code works:

Change the relevant part of your code this way:


Code
print "with: " ;  
my $with = (<>);
chomp $with ;
$with = "\t" if $with eq '"\t"';


Then, when prompted to give the replacement string, type this: "\t" (actually typinq the double quotes).

I assume this will also work if you want a "\n" as the replacement string.


Fred
User

Aug 20, 2012, 7:02 AM

Post #5 of 9 (7489 views)
Re: [Laurent_R] Strange behavior of variable in substitution expression [In reply to] Can't Post

Thank you Laurent !

I also found a similar solution ; But my solution also have some big limitations. Imagine for exemple that you want to replace a space by "\n"."a_chain_of_characters". "\n"
For a simple one character replacement it is ok but for a complex one it does not work.


Code
 
if ($with =~ m/\\([A-Z,a-z])/){
my $char = $1 ;
switch ($char) {
case "t" {$with = "\t" ;}
case "r" {$with = "\r" ;}
case "n" {$with = "\n" ;}
}
}


BillKSmith
Veteran

Aug 20, 2012, 10:03 AM

Post #6 of 9 (7481 views)
Re: [Fred] Strange behavior of variable in substitution expression [In reply to] Can't Post

The ternary operator even works in much older versions of perl.

Code
my $raw_with = <>; 
chomp $raw_with;
my $with = $raw_with eq '\r' ? "\r"
: $raw_with eq '\n' ? "\n"
: $raw_with eq '\t' ? "\t"
: $raw_with eq '\s' ? " "
: $raw_with
;
$string =~ s/$replace/$with/e;

Good Luck,
Bill


Fred
User

Aug 20, 2012, 10:22 AM

Post #7 of 9 (7475 views)
Re: [BillKSmith] Strange behavior of variable in substitution expression [In reply to] Can't Post

Thank you Bill !

But why do you use the <e> option for the s/// operator ? I used <g> instead because I want to enable multiple replacements


Laurent_R
Veteran / Moderator

Aug 20, 2012, 10:32 AM

Post #8 of 9 (7473 views)
Re: [Fred] Strange behavior of variable in substitution expression [In reply to] Can't Post

The e flag enables evaluating the replacement string as a Perl expression. You can use both g and e in the same replacement: $foo =~ s/source/target/eg;

An alternative to the ternary operator or the successive else or case statements would be to use a hash to find the replacement string from the user input.


(This post was edited by Laurent_R on Aug 20, 2012, 10:34 AM)


BillKSmith
Veteran

Aug 20, 2012, 11:40 AM

Post #9 of 9 (7467 views)
Re: [Fred] Strange behavior of variable in substitution expression [In reply to] Can't Post

Sorry, that's a mistake, a leftover from a failed effort. Use /g
Good Luck,
Bill

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives