Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
PerlMonks  

A problem with using the split command

by TASdvlper (Monk)
on Apr 30, 2004 at 20:02 UTC ( [id://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.

Replies are listed 'Best First'.
Re: A problem with using the split command
by Paladin (Vicar) 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

    Change the line to read:

    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 bdalzell (Sexton) on Oct 17, 2016 at 18:16 UTC

    This thread helped me with a problem I was having. Thanks!

    Here is the command line test script that I wrote to test out some options. It allows you to see what response occurs in your system.

    This was tested under Xubuntu linux with a Terminator terminal. For my system I found a DIFFERENCE in behavior between a loaded test file with pipe symbol used as the separator and the pipe symbol being used in a test data line entered from the command line.

    You can also experiment with other potential separators from the command line, but you will have to edit the script and a sample data file to experiment with various separators loaded from a file on disk. A sample |(pipe) separated test file follows the script.

    save program as split-test.pl

    #!/usr/bin/perl -w use strict; # no warnings 'uninitialized'; use English; use utf8; binmode(STDOUT, ":utf8"); print "Using | separated data file loaded from disk;\n | IS escaped in + split\n"; open(IN,"< testdata.csv") || die "cannot open testdata.csv"; my @data = <IN>; chomp(@data); foreach my $line(@data){ foreach my $pwd (split(/\|/, "$line")) { print $pwd,"\n"; }; }; print "hit any key to continue: "; my $ans = <STDIN>; print "Using | separated data file from disk; | NOT escaped in split\n"; foreach my $line(@data){ foreach my $pwd (split(/|/, "$line")) { print $pwd,"\n"; }; }; print "hit any key to continue: "; $ans = <STDIN>; print "following examples use | separated test string in script in \n different ways\n"; print"default separator | NOT escaped in default test string\n"; foreach my $pwd (split(/\|/, "| *foo | *foo1 | *foo2")) { print $pwd,"\n"; }; print"default separator | IS escaped in default test string\n"; foreach my $pwd (split(/\|/, "\| *foo \| *foo1 \| *foo2")) { print $pwd,"\n"; }; print "this section is a loop that allows you to try different separat +ors that you enter from command line\n"; print "you can experiment by using a character alone or proceeding the + character with an \ as an escape'\n for example, using | and then using \ |/.\n"; while(-1){ print "start of loop enter NEW separator: "; my $sep = <STDIN>; chomp($sep); print "separator IS escaped in split command:\n"; print "separator $sep is NOT escaped in split command:\n"; foreach my $pwd (split(/\\$sep/, "$sep *foo $sep *foo1 $sep *foo2")) { print $pwd,"\n"; }; print"\n\t ****\n\n"; print "test separator ($sep) on command line testline\n"; print "using $sep: paste testline here: "; my $testline =<STDIN>; chomp($testline); print "separator IS escaped in split command:\n"; my @testarray = split(/\\$sep/,$testline) ; foreach my $item(@testarray){ print "$item\n"; }; print "separator is NOT escaped in split command:\n"; @testarray = split(/"$sep"/,$testline) ; foreach my $item(@testarray){ print "$item\n"; }; }#endwhile

    sample test file - save as "testdata.csv

    | *foo | *foo1 | *foo2 | *foo4| *foo5 | *foo6
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'))


    SUNADMN
    USE PERL

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others surveying the Monastery: (4)
As of 2024-04-25 12:37 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found