Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
PerlMonks  

Re: String wraps after a replace operation

by Jim (Curate)
on Jan 04, 2014 at 04:40 UTC ( #1069233=note: print w/ replies, xml ) Need Help??


in reply to String wraps after a replace operation

Study this script, then run it.

use strict; use warnings; my $line = <DATA>; my $rt = <DATA>; my $cached_line = $line; $line =~ s/Kept/Resold/g; $line =~ s/,-1,/,$rt,/g; print $line; $line = $cached_line; chomp $rt; $line =~ s/Kept/Resold/g; $line =~ s/,-1,/,$rt,/g; print $line; __DATA__ 2013-06-13 19:00,2013-06-13,2013-06-01,212276,375296,-1,1461599,1434,6 +1073,US,Kept,300x250,1,0,0.25,0.00025 275805

This is what it prints.

2013-06-13 19:00,2013-06-13,2013-06-01,212276,375296,275805
,1461599,1434,61073,US,Resold,300x250,1,0,0.25,0.00025
2013-06-13 19:00,2013-06-13,2013-06-01,212276,375296,275805,1461599,1434,61073,US,Resold,300x250,1,0,0.25,0.00025

Does this help you understand the likely problem? As toolic suggested, the variable $rt probably has more in it than you think it does; namely, a trailing newline. If this is the problem, then the solution is to remove the newline using chomp.

Your problem likely has nothing to do with "wrapping," so forget about using Text::Wrap.

Jim


Comment on Re: String wraps after a replace operation
Select or Download Code
Re^2: String wraps after a replace operation
by aceroraptor (Initiate) on Jan 05, 2014 at 18:31 UTC

    My development environment is on cygwin, the issue was
    due to my test text file containing sample data was
    saved in a file Format = PC and encoding = DOS, once I
    changed it to Format = UNIX and encoding = ANSI my code worked.

      This is a classic:  CR-LF versus LF newline differences in text files between operating systems (DOS/Windows versus Unix/Linux/OS X).

      What text editor did you use to save the file using those formats and character encoding names? I ask this because those names are bogus and confusing, especially the character encoding names. There's no such thing as an encoding named "DOS", and the name "ANSI" is an historic misnomer that is more properly "Windows-1252" or "Code Page 1252" or "CP1252". I suspect the encoding name "DOS" is intended to mean "Code Page 437" or "CP437". But who knows?

      You may need to use a better text editor.

      Unless you're doing something extraordinary to thwart its default behavior (setting the mode of the I/O layer to :raw, for example), Perl should just be doing the right thing with the line terminators in your file. In other words, in general, Perl handles both CR-LF and LF logical newlines correctly and transparently. What version and distro of Perl are you using? ActiveState Perl? Strawberry Perl? Or the Perl that comes with Cygwin?

      Jim

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1069233]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others imbibing at the Monastery: (12)
As of 2014-07-24 07:03 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My favorite superfluous repetitious redundant duplicative phrase is:









    Results (158 votes), past polls