Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic
 
PerlMonks  

Re^2: hash to differentiate type and tokens

by sarvan (Sexton)
on Jul 13, 2011 at 10:42 UTC ( #914094=note: print w/replies, xml ) Need Help??


in reply to Re: hash to differentiate type and tokens
in thread hash to differentiate type and tokens

Hi,

By types and tokens i meant, if i have a word "the" multiple times in a array i need to process it as many times as it is in the array.

But in hash it avoids duplicates by having the token of each word. if a word "the" appears six times in a hash. then type of "the" is only one. but the token of "the" is 6..

Is it correct what i m speaking

  • Comment on Re^2: hash to differentiate type and tokens

Replies are listed 'Best First'.
Re^3: hash to differentiate type and tokens
by Anonymous Monk on Jul 13, 2011 at 11:05 UTC

    A hash associates one key with one value, so this is what you have in %hash

    my $str='finding related pages on the world wide web the web'; my %hash=split(/\s+/,$str); use DDS; Dump( \%hash ); __END__ $HASH1 = { finding => 'related', pages => 'on', the => 'web', wide => 'web' };
    What do you want %hash to contain?
      Hi,
      #! /usr/bin/perl use strict; use warnings; my $str1='It is a guide to action which ensures that the military alw +ays obey the commands of the party.'; chomp($str1); my $str2='It is a guide to action that ensures that the military will +forever heed Party commands is a guide.'; chomp($str2); my @arr1=split(/\s+/, $str1); my $n_occur_in_str1=0; my $n_occur_in_str2=0; print "-"x60,"\n"; print "nth Match\tNo of occurence\t\tMatched Pattern\n"; print "-"x60,"\n"; for(my $i=0; $i<$#arr1;$i++) { my $t1="$arr1[$i] $arr1[$i+1] $arr1[$i+2]"; my @pattern_word_count=split(/\s/, $t1); my $space_count=@pattern_word_count; if($space_count == 3) { if($str2=~/$t1/) { my $occurence=`echo $str2 | grep -o '$t1' |wc - +l`; chomp($occurence); print $i+1,"\t\t",$occurence,"\t\t\t$t1\n"; $n_occur_in_str2+=$occurence; $n_occur_in_str1++; } } } print "\n\nTotal no of occurence in str1 : $n_occur_in_str1\n"; print "\n\nTotal no of occurence in str2 : $n_occur_in_str2\n";

      in the above code i used an array to store the splitted words from sentence and then use each words for further processing

      Here, the problem is when i have repeated words, unwantedly i do computation for same words. so to avoid tat i have been advised to store the words in a hash rather than an array.

      But, i m not clear how i can do that

        in the above code i used an array to store the splitted wor

        Oh, now I remember, this is Comparison between keys in hash all over again

        Ask yourself why anyone would help someone who 1) ignores their advice 2) ignores their questions 3) repeat the same thing, repeatedly?

        I tried to help you before, but every time its either like pulling teeth or like talking to a wall

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others musing on the Monastery: (5)
As of 2023-09-25 10:07 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?