Beefy Boxes and Bandwidth Generously Provided by pair Networks
Come for the quick hacks, stay for the epiphanies.

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

by knobcreekman (Initiate)
on Aug 23, 2013 at 14:10 UTC ( #1050710=perlquestion: print w/replies, xml ) Need Help??

knobcreekman has asked for the wisdom of the Perl Monks concerning the following question:

Hello, long time listener, first time caller here. I am trying to insert records into a db via a data file. Some of the records contain newlines represented by \n that I need to be preserved in the db. I am using prepared statements, so I need the record with \n to be sent as a double-quoted string to get the desired results. The problem is that split strings behave like a single-quoted string (I'm not sure if that's the correct terminology). For example, if I hard code the following in my script it works fine:
@data = (1, 9999, "Here comes \n a new line"); $dbh->execute(@data); Query results: Here comes a new line
But if I have a data file with 1|9999|Here comes \n a new line and then read that data file in such as:
while (<FH>) { @arr = split /\|/; $dbh->execute(@arr); } Query results: Here comes \n a new line
I need the same behavior that I get with the hard-coded string when reading from a data file. Thanks for any help you can offer!

Replies are listed 'Best First'.
Re: Make string that results from split behave like double-quoted string
by chilledham (Friar) on Aug 23, 2013 at 15:56 UTC

    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.

      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.

      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.
Re: Make string that results from split behave like double-quoted string
by glenn (Scribe) on Aug 23, 2013 at 15:50 UTC

    Can you provide more input sample? For instant 2 full records from the file.

    @arr = split /\|/; for (my $x = 0; $x < @arr; $x++) { if ($arr[$x] =~ m/\n/) { $arr[$x] = "\"".$arr[$x]."\""; } } $dbh->execute(@arr);

      chilledham makes a good point, i should have seen it.. the \ and n are being treated as individual characters

      @arr = split /\|/; for (my $x = 0; $x < @arr; $x++) { $arr[$x] =~ s/\\n/\n/g; } $dbh->execute(@arr);

      Been seeing this for (my $x = 0; $x < @arr; $x++) sort of thing around the Monastery of late.

      Is there a reason people prefer it to for (@arr)?

      or is there no reason at all?

        If you've actually seen that, I hope the node had acquired some corrective replies.

        Perhaps what you've actually seen is:

        for (my $i = 0, $i < $#arr, $i++) { #corrections ^ ^^ ^ #strictly speaking, the commas instead of semis are merely my preferen +ces do something...; } <br> <c>

        because the line you posted won't even compile... whereas the example in this node is basicly a C-style loop, possible written that way to show the counter.

        C:\>perl -E "my @arr=qw(foo bar baz);for (my $x = 0; $x < @arr; $x++){ + say $x };" 0 1 2
        My apologies to all those electrons which were inconvenienced by the creation of this post.

        I do not like using $_ I like to know exactly which element is called. Also $#arr returns the last index of the array while @arr returns the number of elements, the difference is $x <= $#arr vs $x < @arr.

Re: Make string that results from split behave like double-quoted string
by jellisii2 (Hermit) on Aug 23, 2013 at 15:56 UTC

    my $string = 'Here comes \n a new line'; $string = eval "qq#$string#"; print $string;

    Probably dangerous.

      Probably dangerous.


        Yes shure, may be.

        But what do you suggest?

        Doesn't that mean that we should avoid eval as well as do?

        And isn't the usage of eval and do sometimes very handy?

        And btw: what about M-x load-file or something similar in emacs/elisp?

        Always risky?

        Best regards, Karl

        «The Crux of the Biscuit is the Apostrophe»

Log In?

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://1050710]
Approved by Old_Gray_Bear
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others rifling through the Monastery: (6)
As of 2023-12-06 00:29 GMT
Find Nodes?
    Voting Booth?
    What's your preferred 'use VERSION' for new CPAN modules in 2023?

    Results (29 votes). Check out past polls.