#! perl -slw use strict; use constant TEMPNAME => 'temp,out.'; my @row = split ' ', scalar <>; my @fhs; open $fhs[ $_ ], '+>', TEMPNAME . $_ for 0 .. $#row; print { $fhs[ $_ ] } $row[ $_ ] for 0 .. $#row; while( <> ) { @row = split; print { $fhs[ $_ ] } $row[ $_ ] for 0 .. $#row; } my( $i, @cCounts, @iRows, @nonZs ) = ( 0, 0 ); for my $fh ( @fhs ) { seek $fh, 0, 0; my $count = 0; while( <$fh> ) { chomp; next unless 0+$_; ++$count; $iRows[ $i ] = $. - 1; $nonZs[ $i ] = $_; ++$i; } push @cCounts, $cCounts[ $#cCounts ] + $count; } print "@$_" for \( @cCounts, @iRows, @nonZs ); close $_ for @fhs; unlink TEMPNAME . $_ for 0 .. $#fhs; __END__ C:\test>791009 sample.dat 0 2 5 9 10 12 0 1 0 2 4 1 2 3 4 2 1 4 2 3 3 -1 4 4 -3 1 2 2 6 1