How do I load this into a hash with one column .You can't have a hash with just one column. Hash elements have to be key-value pairs, although the value may be anything you want (you don't really care anyway), including undef, as in the code suggested by jahero.
I think that, in such a case, most people use either undef or simply 1. The upside of using 1 is that testing the hash value will return a true value, so that you can test existence by a simple look up:
This is marginally useful in one-liners because it is less key stokes than:say "Found it" if $hash{$key};
which is the more standard way of testing a hash for existence.say "Found it" if exists $hash{$key};
You can use map, as shown by jahero, or you can use a for loop. Assuming the file name is stored in the $file_in variable, this may look like this:
Update: fixed a typo: $hash{$line} instead of $hash{line}. Thanks to vighneshmufc for pointing it out.my %hash; open my $INPUT, "<", $file_in or die "Cannot open $file_in $!"; # the +die "..." part is not needed if you use autodie. while (my $line = <$INPUT>) { chomp $line; $hash{$line} = 1; # or undef or anything you like for the value } close $INPUT; # You can use %hash for lookup now
In reply to Re: Load a single column file into a hash
by Laurent_R
in thread Load a single column file into a hash
by vighneshmufc
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |