Problems? Is your data what you think it is? PerlMonks

### A problem with using the split command

by TASdvlper (Monk)
 on Apr 30, 2004 at 20:02 UTC ( #349499=perlquestion: print w/ replies, xml ) Need Help??
TASdvlper has asked for the wisdom of the Perl Monks concerning the following question:

All,

I'm trying to do the following:

```foreach my \$pwd (split('\\','\foo\foo1\foo2')) { ...
And I'm getting the following error: Trailing \ in regex m/\/ at ../modules/perl/CommonFunctions.pm line 2839.

I'm tried using double quotes and a few other things, I can't seem to get it to work. Anybody have any pointers ?

Thx.

Comment on A problem with using the split command
Replies are listed 'Best First'.
Re: A problem with using the split command
by Paladin (Priest) on Apr 30, 2004 at 20:10 UTC
split actually takes a m// for it's first argument, not just a string. Try:
```foreach my \$pwd (split(/\\/,'\foo\foo1\foo2')) {
The reason it was failing with \\ was that in a string, the '\\' turns into a literal \ after interpolation. That single \ is then passed to the regex engine as the RE, which is invalid as \ is special in a RE.
Re: A problem with using the split command
by Belgarion (Chaplain) on Apr 30, 2004 at 20:13 UTC

```foreach my \$pwd (split(/\\/,'\foo\foo1\foo2')) { ...
and it should work correctly.

Update: The following code (as per above, but with an output):

```foreach my \$pwd (split(/\\/,'\foo\foo1\foo2')) {
print \$pwd, "\n";
}
produces the following output.
```__OUTPUT__

foo
foo1
foo2
Notice the extra blank line at the beginning? This occurs because of the initial leading slash in the string to split. You'll likely want to take that into concideration.

Re: A problem with using the split command
by Sandy (Curate) on Apr 30, 2004 at 20:18 UTC
Single quotes or double quotes, I can get it to work with 'four' slashes. See below
```>echo \$perlcode
\$a="x\\y\\z";@b=split('\\\\',\$a);print join(";",@b),"\n"
>perl -e "\$perlcode"
x;y;z
or
```>echo \$perlcode
\$a="x\\y\\z";@b=split("\\\\",\$a);print join(";",@b),"\n"
>perl -e "\$perlcode"
x;y;z
I would have thought that if 4 back-slashes would work with double quotes (becoming \\ instead), then 2 back-slashes would work with single quotes.

Maybe some wise monk would enlighten us?

Sandy

Single quoted type strings do treat \\ and \ followed by the ending delimiter character as special, just as double quoted strings do. It's everything else that they don't treat specially.
Ah HA!

Thanks.

Re: A problem with using the split command
by matija (Priest) on Apr 30, 2004 at 20:15 UTC
You have to double up the backslashes:
```perl -e " print join(', ',split(/\\\\/,'\foo\foo1\foo2'))"
, foo, foo1, foo2
Update: Note to people downvoting me: Unlike some who posted before me, I tried my solution before posting. (See above: it works).
I got this working on the command line like you have in your example, and it works, but in my script it's still not working. Very strange
I got this working on the command line ... but in my script it's still not working

I'm not positive, but I'd say the shell is eating one pair of backslashes. Basically, the shell sees \\\\, passes it in to perl as \\, which gets passed into the RE engine as \. Welcome to the backwhack hell that is multiple layers of interpolation. :)

So, in summary, \\\\ on the shell should be \\ in your script.

Re: A problem with using the split command
by Anonymous Monk on Apr 30, 2004 at 21:21 UTC
maybe it is those unneeded single quotes in your argument to split. perldoc -f split will return some interesting info.
Re: A problem with using the split command
by sunadmn (Curate) on Apr 30, 2004 at 20:14 UTC
I could be way off here but I think you need to escape the '\' like this:
```foreach my \$pwd (split('/\/\','/\foo/\foo1/\foo2'))

USE PERL

Create A New User
Node Status?
node history
Node Type: perlquestion [id://349499]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others examining the Monastery: (15)
As of 2015-10-13 17:08 GMT
Sections?
Information?
Find Nodes?
Leftovers?
Voting Booth?