# Say we have an array of field names # and a corresponding one of lengths # (Decide for yourself if you want 'A' or 'a' here) my $format_str = join '', map { "A$_" } @lengths; while (<>) { my %hash; @hash{@names} = unpack($format_str, $_); }