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

This node falls below the community's threshold of quality. You may see it by logging in.

Replies are listed 'Best First'.
Re: How to find the repeated text in hash
by kennethk (Abbot) on Feb 09, 2015 at 14:57 UTC
    This sounds like an XY Problem. What are you trying to accomplish? What did you try that didn't work? Can you perhaps give us a sample data structure? An example is worth 1000 words. See How do I post a question effectively?.

    My reading of your post says you are looking for a one-to-many mapping in a hash. This is usually done with an array ref: perlreftut, perllol. Perhaps stored with:

    push @{$hash{$key}}, $value;

    #11929 First ask yourself `How would I do this without a computer?' Then have the computer do it the same way.

Re: How to find the repeated text in hash
by Corion (Patriarch) on Feb 09, 2015 at 14:38 UTC

    A hash cannot have keys repeated.

    But maybe, while filling the hash with data, you want to find out if a key already exists in the hash.

Re: How to find the repeated text in hash
by Anonymous Monk on Feb 09, 2015 at 14:16 UTC
Re: How to find the repeated text in hash
by Utilitarian (Vicar) on Feb 09, 2015 at 14:20 UTC
    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."

      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!

      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