Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

Re: recursively building a hash of arrays of scalars

by choroba (Cardinal)
on Sep 30, 2015 at 16:16 UTC ( [id://1143436]=note: print w/replies, xml ) Need Help??


in reply to recursively building a hash of arrays of scalars

my %testHash; my $hashRef = \%testHash; my $destref = \$hashRef->{'a'}; # Just a reference to the value, no de +reference involved. $$destref = "value"; # Assign to the dereference. print $hashRef->{'a'} . "\n";
لսႽ† ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ

Replies are listed 'Best First'.
Re^2: recursively building a hash of arrays of scalars
by aral (Acolyte) on Oct 02, 2015 at 08:06 UTC

    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?" }], # }
      Line 22 is not necessary if you use the more common syntax:
      my %testHash; my $hashRef = \%testHash; $hashRef->{a} = "Hello World!"; my $destref = \$hashRef->{b}; # assign a value to the hash element 'a' $$destref->[0] = 'How'; $$destref->[1] = 'are'; $$destref->[2]{'c'} = "you"; $$destref->[2]{'d'} = "today?";
      لսႽ† ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1143436]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others chanting in the Monastery: (8)
As of 2024-04-19 12:27 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found