use v5.14; use Data::Dumper; my %header; LINE: while () # reading line by line { chomp; # remove the \n at the end if (/^(H\d\d)(.*)/) # ^ beginning of string { $header{$1} = $2; } else { last LINE; # Stop running the LINE loop } } print Dumper \%header; __DATA__ H00 H01 Hi H02 Hello H03 Bonjour D01 D02