Another approach is to leave the string of concatenated key fields as it is and use it alone as the hash key:

Win8 Strawberry 5.8.9.5 (32) Wed 08/31/2022 21:20:10 C:\@Work\Perl\monks >perl use strict; use warnings; use Data::Dump qw(dd); my %config; sub set_config_value { my ($hr_config, # hash ref.: configuration hash to fill $key_seq, # string: multi-key sequence, hyphen-separated $value, # string: value of multi-key sequence ) = @_; # dd '---', '$key_seq', $key_seq, '$value', $value; # for debug push @{ $hr_config->{$key_seq} }, $value; } sub get_config_value { my ($hr_config, # hash ref.: configuration hash to fill @key_seq, # list: multi-key sequence to get value of ) = @_; # dd '===', '$key_seq', \@key_seq; # for debug my $multi_key = join '-', @key_seq; die "key sequence (@key_seq) not in config hash" if not exists $hr_config->{$multi_key}; return $hr_config->{$multi_key}; } for my $ar_add ( [ 'address-virtual-email', 'rpaskudniak', ], [ 'address-virtual-email', 'rasputin', ], [ 'address-virtual-email-foo', 'fred', ], [ '', 'emptystring', ], [ 'bag-end', 'frodo', ], ) { my ($key_sequence, $val) = @$ar_add; set_config_value(\%config, $key_sequence, $val); } dd \%config; dd get_config_value(\%config, qw(address virtual email foo)); dd get_config_value(\%config, ''); dd get_config_value(\%config); ^Z { "" => ["emptystring"], "address-virtual-email" => ["rpaskudniak", "rasputin"], "address-virtual-email-foo" => ["fred"], "bag-end" => ["frodo"], } ["fred"] ["emptystring"] ["emptystring"]
An elaboration of this would be to make the set_config_value() function accept a variety of separator sequences by adding a step like (untested):
    $key_seq =~ s{ \s* (?: => | [-,=]) \s* }{$;}xmsg;
(get_config_value() then has to be changed to join with $; - see perlvar.)


Give a man a fish:  <%-{-{-{-<


In reply to Re: Dynamic addressing in a hash by AnomalousMonk
in thread Dynamic addressing in a hash by rpaskudniak

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.