in reply to Print hash keys and lookup the keys for values in another filr

Hi Magnolia25,

Note that your code contains some errors that prevent it from compiling. I'm going to assume you copied the code you showed out of a larger program and that you've actually got use warnings; use strict; and the missing variable declarations. In the future, please try to provide code that compiles (SSCCE).

First, in last if !line;, you're missing the $ sigil from $line.

Second, note how you've got a comma in (split(/;/, $line)),[1,0]. This is creating several values: the return values of split, plus the anonymous array [1,0]. What you probably meant is (split...)[1,0], which returns a list consisting of the second return value of split followed by the first return value of the split.

If you fix these issues, you've still got a conceptual problem. The way I understand it, you're trying to store a hash with multiple values per key. One way to do this is with a "hash of arrays" or "hash of hashes", which are described with lots of example code in perldsc.

Because this sounds very much like a homework assignment, I'm reluctant to do too much of your work for you :-) However, if you look at perlreftut and perldsc, I hope it will become more clear how you can accomplish your tasks:

  1. Read the file line-by-line, as you already are.
  2. split the line into its columns*.
  3. Store the values in your hash of hashes, such as $hash{$col[1]}{$col[0]}=1;, or into a hash of arrays via push @{ $hash{$col[1]} }, $col[0]; (see push).

* For parsing files like this, it really would be best to use a module like Text::CSV.

Your "Step 2" sounds very much like "Step 1", except with different columns. If you also need to filter the "Step 2" input file, I would suggest that you store the first column from your "Step 1" input file in a hash, and when you're parsing your "Step 2" input file, seeing if that value exists in the hash you created.

Hope this helps,
-- Hauke D

Replies are listed 'Best First'.
Re^2: Print hash keys and lookup the keys for values in another filr
by Lotus1 (Vicar) on Feb 01, 2017 at 16:45 UTC

    I didn't comment on this section before since the program seems to have had very little effort put into it. The last if !line; part will never do anything even if corrected since $line is only declared and assigned a value if the regex matches.

    my $line = $_ if /\bWood\b/; chomp ($line); last if !line;

    Perhaps something like last if /^\s*$/ placed ahead of the my $line = $_ ... would work.

      Hi Lotus1,

      The last if !line; part will never do anything even if corrected

      Turns out I missed it yesterday - the line does do something:

      $ perl -wMstrict -n my $line = $_ if /\bWood\b/; chomp ($line); print "BANG!\n" if !$line; __END__ Hello, Wood World Foobar

      Outputs:

      Use of uninitialized value $line in scalar chomp at - line 2, <> line +2. BANG!

      The ... if !$line gets triggerd when $line is false, which is a case that can happen.

      There's another thing I missed yesterday: my $line = $_ if /\bWood\b/;, to which perlsyn has to say this:

      NOTE: The behaviour of a my, state, or our modified with a statement modifier conditional or loop construct (for example, my $x if ...) is undefined. The value of the my variable may be undef, any previously assigned value, or possibly anything else. Don't rely on it. Future versions of perl might do something different from the version of perl you try it out on. Here be dragons.

      So really, that entire loop body needs a rewrite, maybe:

      while (<$fh>) { chomp; next unless /\bWood\b/; ... }

      Regards,
      -- Hauke D

        Good points. Using my in a conditional statement is something I got burned by a while back and quit doing and forgot why.

      Hi Lotus1,

      The last if !line; part will never do anything even if corrected

      You're right of course, I was at that moment more focused on getting the code compiling, and there was lots more to explain, so I skipped the line that doesn't do anything :-) Update: Oops, the line does do something, see my other reply.

      Thanks,
      -- Hauke D

Re^2: Print hash keys and lookup the keys for values in another filr
by Magnolia25 (Sexton) on Feb 02, 2017 at 04:53 UTC
    in last if !line;, you're missing the $ sigil from $line.

    Sorry that was an typo error