use Modern::Perl '2014'; my @data; ; # discard header line while () { chomp; last if $_ eq 'END PARAMETERS'; # end of data marker, not necessary if reading from a file my ( $station, @fields ) = split /,/; $data[$station] = \@fields; } ; # discard header line while () { chomp; my ( $station, $level ) = split /,/; say "Station $station: salinity $level is $data[$station][$level-10] and temperature $level is $data[$station][$level-4]"; } __DATA__ Station,S_10,S_11,S_12,S_13,S_14,S_15,T_10,T_11,T_12,T_13,T_14,T_15 1,31,29,29,31,29,29,15,14,23,15,14,23 2,33,28,23,33,28,23,17,15,23,17,15,23 3,23,27,33,23,27,33,18,16,23,18,16,23 4,25,26,28,25,26,28,23,14,15,23,14,15 5,26,26,27,26,26,27,23,18,17,23,18,17 6,27,33,31,27,33,31,14,17,18,14,17,18 7,33,29,29,33,29,29,12,18,23,12,18,23 END PARAMETERS Station,Code 1,10 2,11 3,12 4,13 5,14 #### Station 1: salinity 10 is 31 and temperature 10 is 15 Station 2: salinity 11 is 28 and temperature 11 is 15 Station 3: salinity 12 is 33 and temperature 12 is 23 Station 4: salinity 13 is 25 and temperature 13 is 23 Station 5: salinity 14 is 26 and temperature 14 is 18