Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

Comparison between keys in hash

by sarvan (Sexton)
on Jul 06, 2011 at 13:25 UTC ( [id://912973]=perlquestion: print w/replies, xml ) Need Help??

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

Hi everyone,

The below is my script.

#! /usr/bin/perl $str1='It is a guide to action which ensures that the military always +obey the commands of the party.'; chomp($str1); $str2='It is a guide to action that ensures that the military will for +ever heed Party commands is a guide.'; chomp($str2); @arr1=split(/\s+/, $str1); $n=0; %result=(); %output=(); for($i=0; $i<$#arr1;$i++) { $t1="$arr1[$i] $arr1[$i+1] $arr1[$i+2]"; @pattern_word_count=split(/\s/, $t1); $space_count=@pattern_word_count; if($space_count == 3) { if($str2=~/$t1/) { $occurence=`echo $str2 | grep -o '$t1' |wc -l` +; chomp($occurence); $result{$occurence}=$t1; print $i+1,"\t\t",$occurence,"\t\t\t$t1\n"; $n++; } if($str1=~/$t1/) { $occurence1=`echo $str1 | grep -o '$t1' |wc -l +`; chomp($occurence1); $output{$occurence1}=$t1; print $i+1,"\t\t",$occurence1,"\t\t\t$t1\n"; } } } print "\n\nTotal Matches : $n\n";

In this code, i am just comparing $t1 with $str1,$str2 and counting the no.of times it appears.. Then, i store the no.of occurence in two seperate hashes..(%result,%output)

My question now is, i want to compare the keys of first hash with the keys of 2nd hash and find the which has minimum value. This comparison has to be done for all the keys in both hashes.. Any suggestions

Replies are listed 'Best First'.
Re: Comparison between keys in hash
by jethro (Monsignor) on Jul 06, 2011 at 14:27 UTC

    Are you really counting words with a shell command? Check this out:

    my $string="It is a guide to action which ensures that the military al +ways obey the commands of the party"; my %counts; foreach (split(' ',$string)) { $counts{$_}++; } foreach (keys %counts) { print "Word '$_' occurs $counts{$_} times\n"; }

    will print

    Word 'the' occurs 3 times Word 'It' occurs 1 times Word 'a' occurs 1 times Word 'of' occurs 1 times Word 'ensures' occurs 1 times Word 'which' occurs 1 times Word 'commands' occurs 1 times Word 'that' occurs 1 times Word 'is' occurs 1 times Word 'to' occurs 1 times Word 'always' occurs 1 times Word 'military' occurs 1 times Word 'guide' occurs 1 times Word 'party' occurs 1 times Word 'action' occurs 1 times Word 'obey' occurs 1 times

    Oh, and the problem you were asking about: Just use foreach (key ...) on one hash and compare with the second hash. Then do the same with the second hash but only look at keys not in the first hash (as they are the only ones not handled by the first foreach loop).

Re: Comparison between keys in hash
by i5513 (Pilgrim) on Jul 06, 2011 at 14:49 UTC
    Really you should switch to use strict and use warnings
    Here is your code rewritten in 'pure perl', with this notes:
    - my @xx= $str =~ /(xxx)/g # return array with matches
    - scalar @xx # how many items are in @xx
    - you should add use strict your self to the script
    - it is not needed to go to #arr in the loop
    - I really doesn't understand what you want to say in the last paragraph, so my code is only a guess
    See perlretut for more info in first note
    #! /usr/bin/perl $str1='It is a guide to action which ensures that the military always +obey the commands of the party.'; chomp($str1); $str2='It is a guide to action that ensures that the military will for +ever heed Party commands is a guide.'; chomp($str2); @arr1=split(/\s+/, $str1); $n=0; %result=(); %output=(); for($i=0; $i<$#arr1-1;$i++) { $t1="$arr1[$i] $arr1[$i+1] $arr1[$i+2]"; if(@matchs=$str2=~/($t1)/g) { $result{@matchs}=$t1; print $i+1,"\t\t",scalar @matchs,"\t\t\t$t1\n"; $n++; } if(@matchs=$str1=~/($t1)/g) { $output{@matchs}=$t1; print $i+1,"\t\t",scalar @matchs,"\t\t\t$t1\n"; } } print "\n\nTotal Matches : $n\n"; if ((sort keys %output)[0] > (sort keys %result)[0]) { print "result has the minor value\n"; } elsif ((sort keys %output)[0] < (sort keys %result)[0]) { print "output has the minor value\n"; } else { print "Both has the same minor value\n"; }
    PD to perlmonks admins: I really hate to have add <br> in every line :( when I'm commenting or posting

        For both, (MidLifeXis and zek152):

        I was talked before (other monk said me) about like html format when posting here, and I read those tips.
        And I accept, I will tag my post with html tags ...
        But would be wonderful to have an option in your perlmonks settings page where return carriage + newline where respected
        How <br> is short than <p> </p>, I will try to use the first
        Thank you both!

      i5513. You don't have to use <br>. If you use <p> .... </p> the text will be automatically wrapped.

      Hi i5513,

      What i meant in the last statement is. I want to compare the keys at two hashes. let me be specific.

      The whole program splits a sentence and compares each words of the sentence with two sentence i.e($str1,$str2).

      What i needed is: for eg. if "the" word occurs in $str1 two times and $str2 one time. i want find the minimum value i.e is 1.

      Same procedure for all the words in the sentence. The end output should give me the Word string and Its minimum count(after comparing counts in $str1 & $str2)..

        Then I would change your hash to:

        $counting{word}{$word}{str1}=$counter; $counting{word}{$word}{str2}=$counter;

        And then (see here for min function examples):

        foreach my $word (keys %{$counting{word}}) { print "$word\t", min ($counting{word}{$word}{str1}, $counting{word}{$word}{str2}) }

        Sounds like homework.

Re: Comparison between keys in hash
by Anonymous Monk on Jul 06, 2011 at 14:12 UTC

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://912973]
Approved by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others taking refuge in the Monastery: (4)
As of 2024-04-25 12:41 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found