in reply to Re: recursively building a hash of arrays of scalars
in thread recursively building a hash of arrays of scalars
Thank you very much! I am glad I reduced my initial problem to a minimalistic version. With your (all posters) responses, and a little bit of experimenting, I solved my problem and can now create any combination of hash, array and scalars solely working with references:
#!/usr/bin/perl use strict; use warnings; use Data::Dump; my %testHash; # initialize hash my $hashRef = \%testHash; # get a reference to that $hashRef->{'a'} = "Hello World!"; my $destref = \$hashRef->{'b'}; # Just a reference to the value, no de +reference involved. # initialize hash element as empty array $$destref = []; # NOTE: this line is not necessary # assign a value to the hash element 'a' @{$$destref}[0] = "How"; # Assign to the dereference. @{$$destref}[1] = "are"; # Assign to the dereference. # initialize array element as empty hash @{$$destref}[2] = { }; # NOTE: this line *is* necessary @{$$destref}[2]->{'c'} = "you"; # assign values to hash @{$$destref}[2]->{'d'} = "today?"; # dd \%testHash; # Output: # { # a => "Hello World!", # b => ["How", "are", { c => "you", d => "today?" }], # }
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^3: recursively building a hash of arrays of scalars
by choroba (Cardinal) on Oct 02, 2015 at 08:31 UTC |