http://www.perlmonks.org?node_id=980188


in reply to Re: frequency strings 2 files
in thread frequency strings 2 files

#!/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>

Replies are listed 'Best First'.
Re^3: frequency strings 2 files
by ig (Vicar) on Jul 06, 2012 at 04:55 UTC

    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".

Re^3: frequency strings 2 files
by thundergnat (Deacon) on Jul 06, 2012 at 15:49 UTC

    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