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

Mitigating Monks,

Parsing a pipe-delimited text file, line by line.

open(FILE, 'my_file.txt'); while ($line = <FILE>) { @line_parts = split(/\|/, $line); print "<br>$line_parts[0]\n"; } close(FILE);
When the line being parsed is
1.1.2|W|foo|abc
the above code correctly outputs
1.1.2
But if I replace the split delimiter with a variable, like this:
$delimiter = "\|"; ..... @line_parts = split(/$delimiter/, $line);
it outputs
1
I've tried various permutations like :
$delimiter = "|"; ..... @line_parts = split(/\$delimiter/, $line);
to no avail.

What's up?

Thanks.




Forget that fear of gravity,
Get a little savagery in your life.

Replies are listed 'Best First'.
Re: Variable as split delimiter
by ysth (Canon) on Mar 26, 2008 at 15:47 UTC
    When you interpolate variables into regexes, they can contain regex metacharacters. For instance, $foo = '(?:bar|baz)'; /$foo/ matches bar or baz, not the literal "bar|baz". They can also escape those metacharacters: $foo = '(?:bar\|baz)'; /$foo/ matches the literal "bar|baz", not either bar or baz.

    Your problem is your line: $delimiter = "\|"; Because you are using a double-quoted string that does it's own \-escaping, this sets $delimiter to just "|". Then using it in a regex as /$delimiter/ becomes /|/ (match nothing or nothing) which matches between every character, so you get "1", ".", "1", etc. from your split.

    To set $delimiter properly to \|, use $delimiter = "\\|"; or $delimiter = '\|';

      Yep, that did it. That pesky double vs. single quote thing. Thanks.
Re: Variable as split delimiter
by Not_a_Number (Prior) on Mar 26, 2008 at 15:58 UTC

    Let perl do the escaping for you:

    my $delimiter = quotemeta '|';
Re: Variable as split delimiter
by jwkrahn (Abbot) on Mar 26, 2008 at 19:30 UTC

    Use qr to compile the expression first:

    $delimiter = qr/\|/; ..... @line_parts = split $delimiter, $line;