in reply to how to remove empty pipe delimiters in a line which doesn't contain any data

One way is to replace multiple pipes with one pipe using the substitution operator:
use warnings; use strict; while (<DATA>) { s/[|]+/|/g; print; } __DATA__ IFB Northpole||| Alaska||| 907-555-5555 Walmart||| Fairbanks||| Alaska Chicken||| Anchorage||| Alaska||| 907-555-5555 Beef||| Somewhere|||||Over the Rainbow|||907-555-5555

Outputs:

IFB Northpole| Alaska| 907-555-5555 Walmart| Fairbanks| Alaska Chicken| Anchorage| Alaska| 907-555-5555 Beef| Somewhere|Over the Rainbow|907-555-5555

Replies are listed 'Best First'.
Re^2: how to remove empty pipe delimiters in a line which doesn't contain any data
by rpinnam (Novice) on Oct 08, 2015 at 15:41 UTC
    I have tried it before the print statement in @item = @item[s/+/|/g]. it didn't work. Here is my code:
    #!/usr/bin/perl @FILES = glob("*.txt"); foreach my $file (@FILES) { open my $fh, '<', $file; (my $fileName = $file) =~ s/\.[^.]+$//; open(my $output, '>', $fileName.".csv") or die "Could not open fil +e '$fileName' $!"; my @address; my @Detail; print $output ""; while (my $line = <$fh>) { chomp $line; @tokens = split /\|/, $line; chomp(@tokens); $objectName=$tokens[0]; if($objectName ne ""){ my @objectFields; $size = scalar(@tokens); @tokens = @tokens[1..$size]; foreach my $token (@tokens){ $token =~ s/\r|\n//g; push @objectFields,$token; } if($objectName eq "IFB"){ @Detail=@objectFields; }elsif($objectName eq "Walmart"){ @address=@objectFields; }elsif($objectName eq "Chicken"){ my @item = (@Detail,@address,@objectFields); @item = @item[s/[|]+/|/g]; print $output join("|", @item)."\n"; } } } close $output; close $fh; }

      Where in that code did you use the following two lines?

      s/[|]+/|/g; print;