bplegend has asked for the wisdom of the Perl Monks concerning the following question:
Hi, I have an array containing some user entries which we'd like to load into a database. But the database require us to escape each '\' first. Here is the condition. The number of '\' can vary in each element and the position can be anywhere in the element. I come up with the following piece of code which works if I replace '\' with a random character like '0'. It "duplicates" each 0 with another 0 correctly regardless of how many and where it is. However if I replace it with '\', it failed to make a copy of each backslash. I am not sure what went wrong. Can someone shed some light please?
@row=('aa','bb','\\c\c\','d\d'); $separator=','; foreach (@row) { s/(\\+)/$1$1/g; } print join($separator, @row); print "\n";
------------ Result: aa,bb,\\\\c\\c\\,d\\d
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: escape each backslash of each array element
by ikegami (Patriarch) on Jun 24, 2010 at 06:55 UTC | |
|
Re: escape each backslash of each array element
by NetWallah (Canon) on Jun 24, 2010 at 06:05 UTC | |
|
Re: escape each backslash of each array element
by dineed (Scribe) on Jun 24, 2010 at 06:53 UTC | |
|
Re: escape each backslash of each array element
by colwellj (Monk) on Jun 24, 2010 at 06:56 UTC | |
|
Re: escape each backslash of each array element
by Khen1950fx (Canon) on Jun 24, 2010 at 06:59 UTC | |
|
Re: escape each backslash of each array element
by johngg (Canon) on Jun 24, 2010 at 12:24 UTC | |
|
Re: escape each backslash of each array element
by bplegend (Novice) on Jun 24, 2010 at 19:33 UTC |