vighneshmufc has asked for the wisdom of the Perl Monks concerning the following question:

File1
COA213345
COA213345
COA213445
DOB213345
EOA213345
I have a file with one column . How do I load this into a hash with one column . What would be my values for key ? I am to use the column to search in another file

Replies are listed 'Best First'.
Re: Load a single column file into a hash
by AnomalousMonk (Archbishop) on Feb 05, 2018 at 06:16 UTC
    I have a file with one column . How do I load this into a hash with one column .

    Marshall already gave an example of a complete solution here. The only problem with Marshall's code is that it depends on Inline::Files to support the example.

    You want to do more general file I/O. Please see Files and I/O in perlintro and some of the articles in the Input and Output section of the Monastery's Tutorials. After you take a look at these, please come back if you have any more questions.


    Give a man a fish:  <%-{-{-{-<

Re: Load a single column file into a hash
by Laurent_R (Canon) on Feb 05, 2018 at 09:53 UTC
    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.
      thanks a lot However i would love to know why was in $hash{line} = 1.....{line} used
        Yeah, it's a typo, sorry and thanks. I should obviously be $hash{$line}. I fixed it in my original post.
Re: Load a single column file into a hash
by Marshall (Canon) on Feb 05, 2018 at 08:07 UTC
    I liked the post by AnomalousMonk++.
    I used Inline::Files so that I could produce a single post with runnable Perl code.

    Instead of the INLINE FILES, You will need something like this:

    open (FILE1, '<', "pathtofile1") or die "unable to open pathtofile1 $! +"; open (FILE2, '<', "pathtofile2") or die "unable to open pathtofile2 $! +";
      I used Inline::Files so that I could produce a single post with runnable Perl code.

      You can do that without a module by open'ing a reference to a HEREDOC. That would work for multiple input "files" as in your mentioned solution.

      use strict; use warnings; use Data::Dumper; open my $inFH, q{<}, \ <<EOD or die qq{open: < HEREDOC: $!\n}; COA213345 COA213345 COA213445 DOB213345 EOA213345 EOD my %lookup = map { chomp; $_ => 1 } <$inFH>; close $inFH or die qq{close: < HEREDOC: $!\n}; print Data::Dumper->Dumpxs( [ \ %lookup ], [ qw{ *lookup } ] );

      The output.

      %lookup = ( 'EOA213345' => 1, 'COA213445' => 1, 'COA213345' => 1, 'DOB213345' => 1 );

      For the purposes of the OP's request, note that using a hash removes the duplicate value in the input.

      Cheers,

      JohnGG

Re: Load a single column file into a hash
by jahero (Pilgrim) on Feb 05, 2018 at 08:50 UTC

    After reading posts from fellow monks, see the following snippet. Beware, it is a "naive" approach.... but should work for a one time script.

    use strict; use warnings; # we don't want to get burned by stupid mistakes # use autodie; # so that I can omit checking if open failed or succeeded. # not 100% bulletproof, but pretty damned close. use Data::Dumper; # https://metacpan.org/pod/Data::Dumper open my $file, '<', 'file1'; # we assume that the file in question exists, is named 'file1', and is + in current folder my %line_hash = map { chomp; $_ => undef } <$file>; # create a hash out of lines in file. # best read from right to left. # <$file> = get all lines from file as an array # chomp = get rid of end of line character(s) # map = create key value pair, where key is the line # # see http://www.perlmonks.org/?node_id=20505 print Dumper \%line_hash; # does it work?