in reply to frequency strings 2 files

Open the second file, read the numbers into an array. Then, initialize a hash with keys from the array:
undef @hash{@array};
Then open the first file. Read it line by line, for each number, if it exists as a key in the hash, then increment the hash value.

Finally, go over the array and print the corresponding hash value for each member (you might skip the print if the value is not defined as in your example).

What part do you have problems with?

Replies are listed 'Best First'.
Re^2: frequency strings 2 files
by philbertcheese86 (Initiate) on Jul 06, 2012 at 00:05 UTC
    #!/usr/bin/perl open(FILE2, "<testfile2"); @lines2 = <File2>; %hash2 = @hash{@lines2}; open(FILE1, "<testfile1"); @lines1 = <FILE1>; foreach (@lines1) if ($_ == %hash2); %hash2 +=1

    That's as far into your advice as I could get before getting lost. Our book hasn't shown us the @hash{@array} anywhere in examples yet.<\p>

      In @hash{@lines2} you are referring to the hash %hash which you haven't initialized, so %hash2 would end up empty if your program ran - but it doesn't because of other errors, as you may know.

      If your book hasn't shown you @hash{@array} and you are not sure what it means or how to use it, you might be better off solving your problem some other way. There are many ways to solve the problem. I don't see a need for %hash2 myself, nor even for @lines2. Rather than reading all the lines from file 2 into an array then puting the values into a hash, you can read all the lines in a while or foreach loop and deal with them one at a time: "in the given order".

      That's not a mess... THIS is a mess:

      open$x,pop;@x=map{chomp;$_}<$x>;pop@x;map{chomp;$x{$_}++}<>;print map{ +$_,$",$x{$_}||0,$/x2}@x

      when run with your test files yields:

      >perl mess.pl testfile1 testfile2
      5 3
      
      2 2
      
      3 2
      
      1 0
      
      

      SCNR