Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

Re: Comparison between keys in hash

by i5513 (Pilgrim)
on Jul 06, 2011 at 14:49 UTC ( [id://912989]=note: print w/replies, xml ) Need Help??


in reply to Comparison between keys in hash

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

Replies are listed 'Best First'.
Re^2: Comparison between keys in hash
by MidLifeXis (Monsignor) on Jul 06, 2011 at 15:09 UTC

      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!
Re^2: Comparison between keys in hash
by zek152 (Pilgrim) on Jul 06, 2011 at 15:12 UTC

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

Re^2: Comparison between keys in hash
by sarvan (Sexton) on Jul 06, 2011 at 15:06 UTC
    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}) }
        hi i5513,

        Thanks for the code.. And in the context of my whole program,and ur changes in hash implementation, Is that $counter is same as $t1. because in my hash i store keys as ($occurence) and values as $t1.

        And also if possible can u put this in the code of mine. Sorry, i m really weak in perl.. that's why coundn't understand. Plz dont mind.

      Sounds like homework.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others learning in the Monastery: (5)
As of 2024-03-29 04:47 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found