in reply to Filling In "The Gaps" in an Array

I'm not sure if I understood your post or your code, but I think this does what you wanted.

#!/usr/bin/perl -w use strict; my @IDS; my $last = [0,0,0,0,0]; # In case the first line has fields missing while (<DATA>) { my @temp = split/\t+/; # allow multiple tab seperators. # set this element from the last if it's null $temp[$_] eq '' and $temp[$_] = $last->[$_] for 0 .. $#temp; push @IDS, $last = \@temp; # push saving a reference } print "@{$_}" for @IDS; =pod output c:\test>219201 1234 5 20021201 1 0 5678 0 20021202 0 0 5678 0 0 0 10 9120 10 20021211 0 0 6543 5 20021202 0 0 6543 0 0 0 5 6543 0 0 0 5 =cut __END__ 1234 5 20021201 1 0 5678 0 20021202 0 0 0 0 0 10 9120 10 20021211 0 0 6543 5 20021202 0 0 0 0 0 5 0 0 0 5

Okay you lot, get your wings on the left, halos on the right. It's one size fits all, and "No!", you can't have a different color.
Pick up your cloud down the end and "Yes" if you get allocated a grey one they are a bit damp under foot, but someone has to get them.
Get used to the wings fast cos its an 8 hour day...unless the Govenor calls for a cyclone or hurricane, in which case 16 hour shifts are mandatory.
Just be grateful that you arrived just as the tornado season finished. Them buggers are real work.

Replies are listed 'Best First'.
Re: Re: Filling In "The Gaps" in an Array
by tall_man (Parson) on Dec 12, 2002 at 00:38 UTC
    I don't think that multiple-tab delimiters are allowed by the problem as stated. If they were, how could you skip a field in the middle of a line? Also, you didn't allow for fields to be missing at the end of the line. Here is my attempt to fix the problems, with more test data showing the extra error conditions.
    #!/usr/bin/perl -w use strict; my @IDS; my $rowlen = 5; my $last = [(0) x $rowlen]; # In case the first line has fields missin +g while (<DATA>) { chomp; my @temp = split/\t/,$_; # set this element from the last if it's null (!defined($temp[$_]) or $temp[$_] eq '') and $temp[$_] = $last->[$ +_] for 0 .. ($rowlen-1); push @IDS, $last = \@temp; # push saving a reference } print join("\t",@{$_}),"\n" for @IDS; =pod output % perl ddd.pl 0 0 0 0 0 1234 5 20021201 1 0 5678 0 20021202 1 0 5678 0 0 0 10 9120 10 20021211 0 0 6543 5 20021202 0 0 6543 0 0 0 5 6543 0 0 0 5 6543 0 0 0 5 =cut __END__ 1234 5 20021201 1 0 5678 0 20021202 0 0 0 10 9120 10 20021211 0 0 6543 5 20021202 0 0 0 5 0 0 0 5
      If you can assume that there will always be the same amount of tabs, you could write the split as:
      my @temp = split /\t/ => $_, -1;

      and @temp will then always contain the same amount of fields. If the third argument of split is negative, to trailing empty fields will be skipped.

      Abigail

      Good catch++


      Okay you lot, get your wings on the left, halos on the right. It's one size fits all, and "No!", you can't have a different color.
      Pick up your cloud down the end and "Yes" if you get allocated a grey one they are a bit damp under foot, but someone has to get them.
      Get used to the wings fast cos its an 8 hour day...unless the Govenor calls for a cyclone or hurricane, in which case 16 hour shifts are mandatory.
      Just be grateful that you arrived just as the tornado season finished. Them buggers are real work.