in reply to Dynamic addressing in a hash

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:  <%-{-{-{-<