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:

say "Found it" if $hash{$key};
This is marginally useful in one-liners because it is less key stokes than:
say "Found it" if exists $hash{$key};
which is the more standard way of testing a hash for existence.

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:

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
Update: fixed a typo: $hash{$line} instead of $hash{line}. Thanks to vighneshmufc for pointing it out.

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

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.