my $i; while (sysread FH, $digit, 1) # same as: while (sysread(FH, $digit, 1) != 0) # (well, not quite, but close enough here.) { ++$i; # keep track of which digit we're looking at. # deal with the $digit you just got. printf "%04d %d\n", $i, $digit; }