use strict; use warnings; use Data::Dumper; my @headers = split /\s/, ; print Dumper \@headers; my $length = @headers; my @all; while (my $line = ){ my $row = [split /\s/, $line, $length]; push @all, $row; } print Dumper \@all; __DATA__ col1 col2 col3 col4 a b c d 1 2 3 4