When you read your file you construct an array with one element per line where you actually need two. Interpolating an array into a string is only going to result in a single string being assigned to your hash. In any case, qw( ... ) doesn't interpolate so you were assigning the string "@con" to your hash. You can assign an array to a hash but you will get a warning if there aren't an even number of elements..
use strict; use warnings; use Data::Dumper; my $inFile = q{spw612167.dat}; open my $inFH, q{<}, $inFile or die qq{open: $inFile: $!\n}; my @conArr = (); while ( <$inFH> ) { chomp; push @conArr, split m{=>}; } close $inFH or die qq{close: $inFile: $!\n}; my %conHash = @conArr; print Data::Dumper->Dump([\@conArr, \%conHash], [qw{*conArr *conHash}] +);
Here's the output
@conArr = ( 'article', 'art', 'chapter', 'chap', 'section', 'sec' ); %conHash = ( 'chapter' => 'chap', 'article' => 'art', 'section' => 'sec' );
I hope this helps you.
Cheers,
JohnGG
In reply to Re: assign an array into hash
by johngg
in thread assign an array into hash
by aakikce
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |