Beefy Boxes and Bandwidth Generously Provided by pair Networks
Come for the quick hacks, stay for the epiphanies.
 
PerlMonks  

hash to differentiate type and tokens

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

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

Hi there, I want to differentiate between the types and tokens in hash.. for e.g if i have
my $str='finding related pages on the world wide web the web'; my %hash=split(/\s+/,$str);

here, how can i get access to the tokens of hash and types of hash.. basically for duplication i need this.

for eg. "the" and "web" in $str are repeated. so in such cases tokens and types helps.

so, need to know how to use them..

Replies are listed 'Best First'.
Re: hash to differentiate type and tokens
by johngg (Canon) on Jul 13, 2011 at 07:08 UTC

    I'm guessing as well but do you mean something like

    knoppix@Microknoppix:~$ perl -E ' > $str = q{finding related pages on the world wide web the web}; > $hash{ $_ } ++ for split m{\s+}, $str; > printf qq{%-10s - %d\n}, $_, $hash{ $_ } > for sort keys %hash;' finding - 1 on - 1 pages - 1 related - 1 the - 2 web - 2 wide - 1 world - 1 knoppix@Microknoppix:~$

    I hope I've guessed correctly and that this is helpful.

    Cheers,

    JohnGG

Re: hash to differentiate type and tokens
by Anonymous Monk on Jul 13, 2011 at 06:32 UTC
      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

        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?
Re: hash to differentiate type and tokens
by sundialsvc4 (Abbot) on Jul 13, 2011 at 21:14 UTC

    Perl has a few “tricks up its sleeve” to make some coding tasks a little easier.   For example, if you simply write, $somehash{'foo'}++, then the key associated with 'foo' will be an integer that gets incremented ... even if no such key exists previously!   Perl simply “does what you mean.”   It automagically creates a key if necessary, then increments it.   So, if you want to use a hash to count the number of occurrences of words in a string (a very common objective...)   Perl, by design, makes that trivial.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others studying the Monastery: (6)
As of 2023-12-07 14:27 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    What's your preferred 'use VERSION' for new CPAN modules in 2023?











    Results (32 votes). Check out past polls.

    Notices?