Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
PerlMonks  

Re: Make string that results from split behave like double-quoted string

by chilledham (Friar)
on Aug 23, 2013 at 19:56 UTC ( [id://1050725]=note: print w/replies, xml ) Need Help??


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.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others surveying the Monastery: (7)
As of 2024-04-23 06:19 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found