Emacs, scripting and anything text oriented.

Assigning a hash to another hash

Kaushal Modi

A hash can be assigned to a key of another hash using hash reference.

For a hash %HASH, it’s reference is obtained by \%HASH.

In the below example, the %fruit_colors and %veg_colors are assigned to the %food_colors hash.

use Data::Dumper;

my %food_colors = (
                   Fruits     => undef,
                   Vegetables => undef

my %fruit_colors = (
                    Apple  => "red",
                    Banana => "yellow"

my %veg_colors = (
                  "Green pepper"  => "green",
                  Potato          => "white"
print Dumper(\%food_colors);
print Dumper(\%fruit_colors);
print Dumper(\%veg_colors);

$food_colors{Fruits}     = \%fruit_colors;
$food_colors{Vegetables} = \%veg_colors;
print Dumper(\%food_colors);

If you have written a response to this, enter your response post's URL below.

Or, you can send a "comment" webmention (it's OK if you don't know what that means). When asked about your website on an IndieAuth login screen, simply type https://commentpara.de.

Markdown Support**bold**, _italics_, ~~strikethrough~~, [descr](link), `monospace`, ```LANG\nline1\nline2\n``` (Yep, multi-line code blocks too, with syntax highlighting!), auto-hyperlinking.

Webmentions #