Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
PerlMonks  

Re: pipe delimited file problem

by rodion (Chaplain)
on Jan 22, 2007 at 02:56 UTC ( [id://595848]=note: print w/replies, xml ) Need Help??


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).

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://595848]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others admiring the Monastery: (7)
As of 2024-04-18 08:04 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found