my %code; my $file = 'code.txt'; open (READ, $file) || die "Cannot open $file: $!\n"; while (my $line = ){ chomp $line; if ($line =~ /^(\w+)\s+([\w]+)$/i){ (%code) = ( "$2" => $1 ); next; } if ($line =~ /^(\w+)\s+([\w]+)\s+([\w]+)$/i){ (%code) = ( "$2" => $1, "$3" => $1 ); next; } if ($line =~ /^(\w+)\s+([\w]+)\s+([\w]+)\s+([\w]+)$/i){ (%code) = ( "$2" => $1, "$3" => $1, "$4" => $1 ); next; } if ($line =~ /^(\w+)\s+([\w]+)\s+([\w]+)\s+([\w]+)\s+([\w]+)$/i){ (%code) = ( "$2" => $1, "$3" => $1, "$4" => $1, "$5" => $1 ); next; } if ($line =~ /^(\w+)\s+([\w]+)\s+([\w]+)\s+([\w]+)\s+([\w]+)\s+([\w]+)$/i){ (%code) = ( "$2" => $1, "$3" => $1, "$4" => $1, "$5" => $1, "$6" => $1 ); next; } if ($line =~ /^(\w+)\s+([\w]+)\s+([\w]+)\s+([\w]+)\s+([\w]+)\s+([\w]+)\s+([\w]+)$/i){ (%code) = ( "$2" => $1, "$3" => $1, "$4" => $1, "$5" => $1, "$6" => $1, "$7" => $1 ); next; } } foreach (keys %code){ print $_, "\t", $code{"$_"}, "\n"; }