Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options

Merge hash keys

by kdarbs (Novice)
on Oct 30, 2013 at 08:31 UTC ( #1060290=perlquestion: print w/replies, xml ) Need Help??
kdarbs has asked for the wisdom of the Perl Monks concerning the following question:

I have %hash = ( anee => '1', jane => '0', neen => '2', ); How can I merge these to have janeen.

Replies are listed 'Best First'.
Re: Merge hash keys
by hdb (Monsignor) on Oct 30, 2013 at 08:55 UTC

    My interpretation of your question: Look at the keys of the hash in the order of the values of the hash and join the keys (as strings) with overlaps.

    Under this assumption the steps are:

    • Sort the keys by the values.
    • Intialize your merged string with the first key.
    • Determine the maximum overlap of the merged string with the next key. Start with the maximum overlapping length, use substr to retrieve the end of the merged string and the beginning of the next key and compare them. Reduce overlap until a match is found and append the rest to the merged string. If no match is found, just concatenate the two strings.
    • Repeat with all keys.

Re: Merge hash keys (syntax or steps)
by Anonymous Monk on Oct 30, 2013 at 08:48 UTC
Re: Merge hash keys
by QM (Parson) on Oct 30, 2013 at 08:52 UTC
    Welcome to the Monastery, kdarbs!

    What have you tried? See How (Not) To Ask A Question.

    It seems you could naively use substr, perhaps like this (untested):

    # rename %hash to %str2pos, as that seems an accurate name my %str2pos = ( anee => '1', jane => '0', neen => '2', ); my $result; for my $string(keys %str2pos) { substr($result,$str2pos{$string},length($string),$string); } print "$result\n";

    Quantum Mechanics: The dreams stuff is made of

Re: Merge hash keys
by hdb (Monsignor) on Oct 30, 2013 at 11:54 UTC

    Instead of substr you could also use regular expressions. Concatenate all your strings in the correct order with a delimiter character that is not part of the strings, eg. #. Then find the same sequence of characters to the left and right of each delimiter and remove one side. It could look like this:

    my %hash = ( anee => '1', jane => '0', neen => '2', ); my $merge = join '#', sort { $hash{$a} <=> $hash{$b} } keys %hash; $merge =~ s/([^#]*)#(?=\1)//g; print "$merge\n";
Re: Merge hash keys
by boftx (Deacon) on Oct 30, 2013 at 23:38 UTC

    Interesting homework question. Hint: sort on values.

    The answer to the question "Can we do this?" is always an emphatic "Yes!" Just give me enough time and money.
Re: Merge hash keys
by kdarbs (Novice) on Oct 31, 2013 at 00:33 UTC
    Thank you all. hdb's response was exactly what I needed. I will be more specific when asking a question next time.
      Wait... what? hdb's answer was exactly what you needed? Don't get me wrong, it's clever and there's a good chance it's fast, but anybody reading your code will have no idea what the hell is going on. I have to agree with some of the other posters that some kind of sort with substring is the right way to go here. Even if it's slower, at least it properly conveys your intent.
Re: Merge hash keys
by sundialsvc4 (Abbot) on Oct 30, 2013 at 14:24 UTC

    shrug...   You can’t hope for any sort of meaningful answer until you make it clear what you want to do.   I only see three keys here, each with different values.   No rule by which to piece them together.   No idea what sort of rule you would consider “correct.”   Not even a guess.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1060290]
Approved by hdb
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others browsing the Monastery: (7)
As of 2018-03-17 14:59 GMT
Find Nodes?
    Voting Booth?
    When I think of a mole I think of:

    Results (224 votes). Check out past polls.