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


In reply to Re: Print hash keys and lookup the keys for values in another filr by haukex
in thread Print hash keys and lookup the keys for values in another filr by Magnolia25

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.