Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw

String wraps after a replace operation

by aceroraptor (Initiate)
on Jan 03, 2014 at 22:19 UTC ( #1069210=perlquestion: print w/replies, xml ) Need Help??
aceroraptor has asked for the wisdom of the Perl Monks concerning the following question:

Objective: To match on a pair of values in a csv file and replace a -1 value to a code
Issue: After my replace operation the resulting string wraps around itself around character 70 and not sure how to prevent this. Tried using Text::Wrap to increase columns before wrap
i.e. line of data = 2013-06-13 19:00,2013-06-13,2013-06-01,212276,375296,-1,1461599,1434,61073,US,Kept,300x250,1,0,0.25,0.00025

$line =~ s/Kept/Resold/g; # This replace works fine $line =~ s/,-1,/,$rt,/g; # Variable $rt = 275805

After replace operation the line of data = 1461599,1434,61073,US,Resold,300x250,1,0,0.25,0.00025276,375296,275805

Replies are listed 'Best First'.
Re: String wraps after a replace operation
by toolic (Bishop) on Jan 03, 2014 at 22:29 UTC
      Thank you Jim and Toolic there is something in the $rt variable which is set according to a value in a hash of hashes. I will be able to troubleshoot tomorrow. Regards
Re: String wraps after a replace operation
by Jim (Curate) on Jan 04, 2014 at 04:40 UTC

    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
    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.


      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?


Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1069210]
Approved by toolic
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others meditating upon the Monastery: (7)
As of 2018-02-21 12:00 GMT
Find Nodes?
    Voting Booth?
    When it is dark outside I am happiest to see ...

    Results (279 votes). Check out past polls.