in reply to how to remove empty pipe delimiters in a line which doesn't contain any data
Hello rpinnam,
For reading a CSV or similar file, a module such as Text::CSV is usually the best idea. Yes, it may seem like overkill, but, unless you can be certain there will never be any edge cases (string-embedded delimiters, embedded newlines, ...) in your data files, you’ll benefit from using a dedicated module in the long run. Here’s a demonstration adapted from toolic’s answer and the module documentation:
#! perl use strict; use warnings; use Text::CSV; my $csv = Text::CSV->new({ sep_char => '|' }) or die "Cannot use CSV: " . Text::CSV->error_diag; while (my $line = <DATA>) { $csv->parse($line); my @fields = grep { !/^\s*$/ } $csv->fields; print join('|', @fields), "\n"; } $csv->eof or $csv->error_diag; __DATA__ IFB Northpole||| Alaska||| 907-555-5555 Walmart||| Fairbanks||| Alaska Chicken||| Anchorage||| Alaska||| 907-555-5555 Beef||| Somewhere|||||Over the Rainbow|||907-555-5555
Output:
0:31 >perl 1397_SoPW.pl IFB Northpole| Alaska| 907-555-5555 Walmart| Fairbanks| Alaska Chicken| Anchorage| Alaska| 907-555-5555 Beef| Somewhere|Over the Rainbow|907-555-5555 0:31 >
Hope that helps,
| Athanasius <°(((>< contra mundum | Iustus alius egestas vitae, eros Piratica, |
|
|---|