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