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

I'm trying to split based on an ASCII character #215, but it's not working.
($1, $2, $3) = split "?";

Replies are listed 'Best First'.
Re: Trying to split on '?'
by seattlejohn (Deacon) on May 19, 2002 at 22:39 UTC
    Others have mentioned that if you want to split using '?' as a delimiter, you need to escape it first, though I'm not convinced that's really your problem here given that ASCII character 215 is not actually a ?.

    The problem I suspect you're encountering is that the variables $1, $2, $3, etc. have special meanings in Perl -- they refer to portions captured within the parentheses of a pattern match. Try this instead:
    ($one, $two, $three) = split quotemeta($delimiter);

    where $delimiter is ASCII 215 or whatever you're intending to use. The quotemeta will escape any characters that might have special meanings when used in a regular expression. (To be more precise, it escapes any non-"word" character.)

    Also note that split used in this fashion is implicitly operating on the contents of the $_ variable. If you're not familiar with the use of $_, you can sometimes make your life easier by explicitly indicating what variable you want to split:
    ($one, $two, $three) = split quotemeta($delimiter), $stuff_to_split;

    Good luck!

Re: Trying to split on '?'
by grep (Monsignor) on May 19, 2002 at 21:40 UTC

    The expressions you use in split (unlike join) are regex's. '?' is a special character (zero or one) in a regex. You will need to escape '?' with a '\' (Ex. "\?").

    You should use -w or use warnings because what you did would have shown up as the warning

    Quantifier follows nothing before HERE mark in regex m/? << HERE


    grep
    Unix - where you can throw the manual on the keyboard and get a command
Re: Trying to split on '?'
by tstock (Curate) on May 19, 2002 at 21:37 UTC
    split splits on a pattern, even if you use double or single quotes. try:
    ($1, $2, $3) = split /\?/;
    tstock
Re: Trying to split on '?'
by graff (Chancellor) on May 19, 2002 at 23:12 UTC
    Kudos to seattlejohn. So, is "#215" a decimal ascii value? or octal? (probably not hex, since that would only need two digits, the first of which would be a letter in this case). If you know the octal or hex value of the delimiter character, the easiest expression for split (IMO) is:
    @arry = split /\0215/; # or /\x8d/ if 215 is the octal value @arry = split /\0327/; # or /\xd7/ if 215 is the decimal value

      split chr(215), $str

      cheers

      tachyon

      s&&rsenoyhcatreve&&&s&n.+t&"$'$`$\"$\&"&ee&&y&srve&&d&&print

        That did it. Thanks mate.
Re: Trying to split on '?'
by tachyon (Chancellor) on May 19, 2002 at 23:22 UTC
    my $str = 'foo' . chr(215) . 'bar' . chr(215) . 'baz'; print $str, "\n"; my ($a,$b,$c) = split chr(215), $str; print "As simple as $a $b $c"; __DATA__ foo×bar×baz As simple as foo bar baz

    cheers

    tachyon

    s&&rsenoyhcatreve&&&s&n.+t&"$'$`$\"$\&"&ee&&y&srve&&d&&print