http://www.perlmonks.org?node_id=595848


in reply to pipe delimited file problem

For what it's worth, I second imp's suggestion to use split. Even though the split version
$_ = join '|', map {$_ ||= 'empty' } split('\|',$_,-1);
is a little longer than the regex
s/(?<=\|)\|/empty|/g;
It's closer to how one thinks of a delimited record, and so it's easier to change if you need to.

One difference is that the split version puts the 'empty' string in the first field, if it's empty, while the regex version doesn't, So that may be the most significant difference for you.

Update: Corrected missing negative limit in split. See graff's comment.

Replies are listed 'Best First'.
Re^2: pipe delimited file problem
by graff (Chancellor) on Jan 22, 2007 at 03:34 UTC
    But if you're going to use split in a situation like this, be sure you tell it not to drop "trailing nulls":
    $_ = join '|', map { $_ ||= 'empty' } split( /\|/, $_, -1 );
    The third arg to split() (set to "-1") is important here. Without it, one or more instances of "|" at the end of the string will simply be ignored, and the output could have a variable number of records per line (which might cause trouble downstream).