#!/usr/bin/perl -w use strict; my @IDS; my $rowlen = 5; my $last = [(0) x $rowlen]; # In case the first line has fields missing while () { 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