in reply to Make string that results from split behave like double-quoted string

Something like this should work:

#!/usr/bin/env perl use strict; use warnings; use Data::Dumper; while (<DATA>) { chomp; my @ar = split(/\|/ => $_); s/\\n/\n/g for @ar; warn Dumper @ar; } __DATA__ 1|99999|Here comes \n a new line

Edit: the fat arrow in the split is just a personal preference of mine. I find it's easier to look at, though split(/\|/, $_) or split /\|/ work just as fine as well.

Replies are listed 'Best First'.
Re^2: Make string that results from split behave like double-quoted string
by protist (Monk) on Aug 23, 2013 at 20:11 UTC

    I would imagine it would be more efficient to do the substitution before the split.

    No needless iterating then.

    Something like this:

    while (<DATA>) { chomp; s/\\n/\n/g; ## Avoiding $_ is sometimes my @ar = split /\|/; ## even more fun than using warn Dumper @ar; ## it. ;P }

      ++ regarding the substitution before the split.

      Regarding use of $_, I do tend to avoid that myself, however it can make some examples more clear. Notice I left it out of the chomp and another example in the edit.

Re^2: Make string that results from split behave like double-quoted string
by knobcreekman (Initiate) on Aug 23, 2013 at 20:56 UTC
    Thanks, that works. I didn't try substituting in the code because I had already tried using various forms in the data file. Is there a way I can change the \n in the data file that will prevent a code change? Just curious.