Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

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

by glenn (Scribe)
on Aug 23, 2013 at 19:50 UTC ( [id://1050723]=note: print w/replies, xml ) Need Help??


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

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);
  • Comment on Re: Make string that results from split behave like double-quoted string
  • Download Code

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:17 UTC

    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.

        The "line" I posted was actually an excerpt, and identical

        to the one in your second example. I am confused about

        what you think I did wrong.

        I was actually referring to glenn's code, which

        made no use of the counter aside from indexing.

        Even if you were to want to show a counter, there are other ways

        my $c = 0; for(@a){ print "$c: $_\n"; $c++; }

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

    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);

Log In?
Username:
Password:

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

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

    No recent polls found