in reply to Re^2: identifying null fields in bar delimited records
in thread identifying null fields in bar delimited records

holli and wfsp, great answers. Now, I'd like to make this snippet of code more general by turning the delimiter into a variable. But doing so doesn't work (I'm struggling to understand regular expressions).
my $delim = '|'; my $line = "a||c|\t| |d||e"; my @fields = split(/\\$delim/, $line); # output # null fields: 0 # at field:

Replies are listed 'Best First'.
Re^4: identifying null fields in bar delimited records
by wfsp (Abbot) on May 31, 2005 at 12:50 UTC
    my $delim = qr/\|/; my $line = "a||c|\t| |d||e"; my @fields = split($delim, $line);

    The qr operator quotes and compiles its STRING as a regular expression.
    split splits on a pattern (regex).

    update:

    Added explanation.

Re^4: identifying null fields in bar delimited records
by holli (Abbot) on May 31, 2005 at 12:53 UTC
    I would put the escaping backslash into $delim, so you are truly independent. Besides that your code works fine and splits the string as intended.
    my ($delim, $line, @fields); $delim = '\|'; $line = "a|c||b"; @fields = split(/$delim/, $line); print join ("*", @fields), "\n"; $delim = ';'; $line = "a;c;;b"; @fields = split(/$delim/, $line); print join ("*", @fields), "\n"; #a*c**b #a*c**b
    What else did you expect?


    holli, /regexed monk/