Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Assuming that the first hash (template) contains all the possible values that the second can have, you can use this approach, which may not be the most efficient but is simple and doesn't require external modules:</p> <pre><code>use strict; my $template = { 'Hotkeys' =&gt; [ 'key', 'keyCR', 'keyHighlight', 'updated' ], 'Actions' =&gt; [ 'action', 'actionCR', 'actionHighlight' ], 'Settings' =&gt; [ 'chbAcronym', 'chbCompleted' ], 'NewSetting' =&gt; [ 'NewValue' ] }; my $user = { 'Hotkeys' =&gt; [ 'key', 'keyCR', 'keyHighlight' ], 'Actions' =&gt; [ 'action', 'actionCR', 'actionHighlight' ], 'Settings' =&gt; [ 'chbAcronym', 'chbCompleted' ] }; #take all user keys so that we don't perform a join in each iteration my $all_user_keys = join(' ',keys %$user); #loop all template keys to see what's missing from user keys foreach my $template_key( keys %$template ) { #this will return a true value if the template key also exists in user hash my $key_exists_in_user = ( $all_user_keys =~ m/$template_key/ ); #if it exists, perform a second loop for the values of the array if ($key_exists_in_user) { #take all user values so that we don't perform a join in each iteration my $all_user_values_of_key = join(' ', @{$user-&gt;{$template_key}}); #loop all values of template key, to see what's missing from user values foreach my $template_key_value (@{$template-&gt;{$template_key}}) { #if value is not found, do what you want with it unless( $all_user_values_of_key =~ m/$template_key_value/ ) { print " -- value '$template_key_value' does not exist in user key '$template_key'. will add it now\n"; push @{$user-&gt;{$template_key}}, $template_key_value; } } #else, hash key is not found, so do what you want with it } else { print "hash key '$template_key' does not exist in user settings. Will add it now\n"; $user-&gt;{$template_key} = $template-&gt;{$template_key}; } } </code></pre> <p>I used your example hashes and I also assumed that your hashes are actually hashrefs (copy pasted as they were)</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload