jclaudio has asked for the wisdom of the Perl Monks concerning the following question:

hi, I'm a newbee on perl and I have a problem using the simple function split I want to use a delimiter as a parameter given by a comand line( i use getopts, and it works ). I store it in the scalar $delimiter in my script, and I want to split a line with it. I tried several syntaxes but none is working :
@array = split ( /$delimiter/, $line ); @array = split ( /"$delimiter"/, $line ); @array = split ( $delimiter, $line ); @array = split ( '$delimiter', $line ); @array = split ( /\$delimiter/, $line );
when i do a print $delimiter, it prints the right thing : | (for example ) and when I do : @array = split ( /\|/, $line ); it works well ! but 'cause the delimiter can be different : or ; I have to pass It to my script as a parameter Has anybody an idea about the split syntax , in that case ? I've been searchin' in the perl cookbook, but nothing helps I need help ! thanks

Replies are listed 'Best First'.
Re: problem with split function
by Limbic~Region (Chancellor) on Oct 20, 2003 at 16:52 UTC
    Anonymous Monk,
    I believe quotemeta will solve your problem.
    #!/usr/bin/perl -w use strict; # Your code $delimiter = quotemeta $delimeter; my @array = split ( /$delimiter/, $line );
    If this does not correct your problem, please provide the sample data and the whole block of code you are trying.

    Cheers - L~R

      YES !! It works !! quotemeta.. I could have been searchin' for this tip for years... thank you very much for your pretty fast and right answer ! that's what I call reactivity ! you rock !!

        And to make that even easier, use \Q in your regex to invoke quotemeta inline. No need to alter $delimiter or use an intermediary variable.

        split /\Q$delimiter/, ...
Re: problem with split function
by etcshadow (Priest) on Oct 20, 2003 at 17:01 UTC
    @array = split(/\Q$delimiter\E/, $line);
    Always wrap scalars which you mean to be interpretted as literal values in regexes with \Q...\E, that way you are safe against the literal containing meta-characters.

    ------------
    :Wq
    Not an editor command: Wq
Re: problem with split function
by tcf22 (Priest) on Oct 20, 2003 at 17:05 UTC
    Use quotemeta. This will escape all non-alphanumeric characters. The problem you are running into is the "|" is being interpreted as an "or" in the regex, so you split at "nothing" or "nothing", so it splits the string at every character(equivalent to split(//, $str). Code Sample:
    my $str = "1|2|3"; my $delimiter = #get delimiter $delimiter = quotemeta($delimiter); print "$_\n" foreach(split(/$delimiter/, $str)); __OUTPUT__ 1 2 3

    - Tom

Re: problem with split function
by graff (Chancellor) on Oct 21, 2003 at 04:30 UTC
    Since you are using a command line option to specify the delimiter, let me suggest that you try avoiding the use of quotemeta -- even though this happens to be the shortest answer to your original question.

    Instead, provide the delimiter string on the command line in such a way that it will be properly interpreted within the script. For example, in bash (or other unix/bourne-like shell), you could do:

    your_script -d '\|' foo bar
    (or whatever the command line is supposed to look like) -- note that quotation marks are placed around the delimiter string, so that the backslash and vertical bar are passed to the script without being interpreted by the shell. If your script does not use quotemeta, then the regex for the split will be:
    /\|/
    which is exactly what you want. Now, suppose you really want to use some "magic" regex characters for your delimiter string... e.g. your input file has lines like this:
    foo:bar;baz;blork blix etc foo2:bar3;bazx;blrk blx and-so-on
    and you need to split on colon, semi-colon and space. The command-line option for the delimiter would then be:
    your_script -d '[:; ]' that.file
    If you used quotemeta on the delimiter string inside your script, this sort of flexibility would not be possible.