use strict; use warnings; my %col_idx; open(my $result_fh, '<', 'file') # 3-arg safer or die("Unable to open input file: $!\n"); # $! meaningful while (<$result_fh>) { chomp(); # chomp safer my @fields = split(/;/, $_); if ($. == 1) { @col_idx{@fields} = 0..$#fields; } else { # Do what you want here. print "$fields[$col_idx{name}], "; print "$fields[$col_idx{postcode}]\n"; } }