Hi Monks,

I am reading in a text file (that I am free to edit) which contains lines of either text or numbers. As I read in, line by line, I push the line into @numbers or @text, as appropriate. I am using both strict and warnings.

My question concerns handling of "\n" in the input file. In one of the lines of text in the file, I find the string:

"some text here \n some text here."

When I look at @text after I am done with the file, that array element reads:

"some text here \\n some text here."

I've pinpointed the change to the following snippet of code surrounded by Dumper calls:

if ($x>=2 && $line !~ /^Text/) { $string .= $line; print "In 'if, line ne text', string is:\n$string\n"; $x++; next; } if ($x!=0){ push (@texts, $string); # whether I use this line or the one afte +r, the result is the same # $texts[$i] = $string; print "In if, using push, \@texts is:\n"; print Dumper(@texts), "\n"; $string = ''; $i++; $x = 0; goto TT; }

The output is, as mentioned above:

In 'if, line ne text', string is: some text here\n some text here In if, using push, @texts is: $VAR1 = 'some text here.'; etc... $VAR10 = 'some text here.\\n some text here.';

So, my question is, why is the newline character having an extra escape character added to it when I push it into @texts, and how do I keep the \n but remove the extra '\'?

Thanks!


In reply to Escape character being escaped in array push by mdunnbass

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • 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:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.