in reply to Putting text file into a hash
Tip: Put your code inside <c>...</c> tags when you post it. It preserves the spacing and escapes special characters for you.
but it doesn't seem to work.
That's not very helpful. Tell us what it does that is shouldn't do, or what it doesn't do that it should do.
Your program works as is. The only problem I see is that you are chomping the wrong variable. chomp works on $_ when no argument is specified. Use chomp($line);.
open FILE1, "text_file_with_words.txt" or die; my %hash; while (my $line=<FILE1>) { chomp($line); (my $word1,my $word2) = split /:/, $line; $hash{$word1} = $word2; } use Data::Dumper; print Dumper \%hash;
$VAR1 = { 'word1' => 'word2', 'word5' => 'word6', 'word3' => 'word4' };
|
|---|