Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>If I understand your issue correctly, you want to add items from the template hash to the user settings' hash <em>only if</em> those items from the template hash do not exist within the users' settings hash.</p> <p>We can take advantage of Perl's <a href="http://en.wikipedia.org/wiki/Autovivification" rel="nofollow">Autovivification</a> which will create the complete data structure within the user settings' hash if an item in that hash doesn't exist. Consider the following:</p> <pre><code>use strict; use warnings; use Data::Dumper; my %template = ( 'Hotkeys' =&gt; [ 'key', 'keyCR', 'keyHighlight', 'updated' ], 'Actions' =&gt; [ 'action', 'actionCR', 'actionHighlight' ], 'Settings' =&gt; [ 'chbAcronym', 'chbCompleted' ], 'NewSetting' =&gt; [ 'NewValue' ] ); my %userSettings = ( 'Hotkeys' =&gt; [ 'key', 'keyCR', 'keyHighlight' ], 'Actions' =&gt; [ 'action', 'actionCR', 'actionHighlight' ], 'Settings' =&gt; [ 'chbAcronym', 'chbCompleted', 'aUserSetting' ] ); updateUserSettings( \%template, \%userSettings ); print Dumper \%userSettings; sub updateUserSettings { my ( $templateHash, $settingsHash ) = @_; for my $key ( keys %$templateHash ) { $settingsHash-&gt;{$key}-&gt;[$_] //= $templateHash-&gt;{$key}-&gt;[$_] for 0 .. $#{ ${$templateHash}{$key} }; } } </code></pre> <p>Output (a dump of <code>%userSettings</code> after the 'update'):</p> <pre><code>$VAR1 = { 'Hotkeys' =&gt; [ 'key', 'keyCR', 'keyHighlight', 'updated' ], 'Actions' =&gt; [ 'action', 'actionCR', 'actionHighlight' ], 'NewSetting' =&gt; [ 'NewValue' ], 'Settings' =&gt; [ 'chbAcronym', 'chbCompleted', 'aUserSetting' ] } </code></pre> <p>Note that <code>%userSettings</code> is only updated with missing <code>%template</code> information and nothing else is disturbed.</p> <p>The subroutine <code>updateUserSettings</code> uses Perl's <em>defined-or</em> (<code>//=</code>) operator as it iterates through all the keys of <code>%template</code>, so <code>%userSettings</code> isn't changed if a key/value already exists, otherwise it updated.</p> <p>Hope this helps!</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