Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

Re: How to find the repeated text in hash

by Utilitarian (Vicar)
on Feb 09, 2015 at 14:20 UTC ( #1116060=note: print w/replies, xml ) Need Help??


in reply to How to find the repeated text in hash

Assuming you mean to count the number of occurrences of the same value for different keys in a hash (%names in the example below)
... my %name_count; for my $name (values %names){ $name_count{$name}++; } for my $name (sort {$name_count{$a}<=>$name_count{$b}} keys %name_coun +t){ print "$name occurs $name_count{$name} times in the \%names hash\n"; }
print "Good ",qw(night morning afternoon evening)[(localtime)[2]/6]," fellow monks."

Replies are listed 'Best First'.
Re^2: How to find the repeated text in hash
by Anonymous Monk on Feb 09, 2015 at 14:26 UTC

    It's also possible that codewalker wants to add the key "Name" to a hash three times, and wants to detect the collision and re-name the second and third keys to "Name2" and "Name3". Your guess is as good as mine, that's the problem!

Re^2: How to find the repeated text in hash
by BillKSmith (Prior) on Feb 10, 2015 at 14:54 UTC
    My guess is that the OP wants to edit the values of an existing hash. Each value that is not unique should be made so by appending an integer.
    use strict; use warnings; use Data::Dumper; my %example = ( a => 'red', b => 'blue', c => 'red', d => 'green', e => 'red', ); my %desired = ( a => 'red', b => 'blue', c => 'red1', d => 'green', e => 'red2', ); my %seen; while (my ($letter, $color) = each %example) { if ($seen{$color}++) { $example{$letter} = $color . $seen{$color}; next; } $example{$letter} = $color } print Dumper( \%desired, \%example );

    UPDATE: Added example.

    Bill
      This is a minimalist answer, and is easily ambiguous:
      Each value that is not unique should be made so by appending an integer.

      Perhaps you mean something like:

      Before inserting a hash key, if it already exists, string increment the key and try again.

      -QM
      --
      Quantum Mechanics: The dreams stuff is made of

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others exploiting the Monastery: (4)
As of 2020-10-22 21:49 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    My favourite web site is:












    Results (230 votes). Check out past polls.

    Notices?