How to insert a hash hash into a hash?

I would like to start with an empty hash and then insert a hash of the same type of hashes into the hash.

#!/usr/bin/perl

use strict;
use warnings;

my %h = {};

$h{"1"} => {
    a => -1,
    b => -1,
    c => [
    {
        d => -1,
        e => -1,
    },
    ],
};

However it gives

Useless use of hash element in void context at ./hash.pl line 8.
Useless use of anonymous hash ({}) in void context at ./hash.pl line 8.
Reference found where even-sized list expected at ./hash.pl line 6.

This is a kind of database that I would like to create, where I can insert type deletion structures $h{"1"}.

Any ideas how to do this?

+3
source share
2 answers

You use to initialize the hash %h = ().
{}is a reference for empty anonymous hashing. Try the following:

my %h = ();

$h{"1"} = {
    a => -1,
    b => -1,
    c => [{
        d => -1,
        e => -1,
    }],
};
+9
source

NOTE. Links are always scalar, since they contain an address (a kind to think carefully)

, , perl , , . .

,

, :

$ra = [ ];

, :

$rh = { };

, .

, , - ,

my %h={};

, .

,

:

#!/usr/bin/perl

use strict;
use warnings;

my $h={"1" => {
            a => -1,
            b => -1,
            c => [
                 {
                   d => -1,
                   e => -1,
                 },
                 ],
              }
};

Perl .. Perl:):)

+3

Source: https://habr.com/ru/post/1793820/


All Articles