3

Just an academic question regarding nesting capabilities.

For example:

%inner = (1, "monday", 2, "tuesday"...);
%outer = ("hello", 1, "days", %inner);
3
  • 1
    Yes — you can nest arrays (or, at least, references to arrays) more or less arbitrarily. And that applies to both ordinary arrays and hashes (associative arrays). Commented Jul 13, 2021 at 19:57
  • See perldoc.perl.org/perldsc Commented Jul 13, 2021 at 21:16
  • For a gentle tutorial intro to this topic, see: Mark's very short tutorial about references Commented Jul 14, 2021 at 2:35

2 Answers 2

11

The value in a hash is always a scalar, but it can be a hash reference.

my %outer = (hello => 1,
             days  => \%inner);

Or you can enter an anonymous hash directly:

my %outer = (hello => 1,
             days  => {1 => 'Monday',
                       2 => 'Tuesday',
                       ...});

Withour a reference, the "nested" hash is flattened, which is sometimes used to override default values:

my %conf = (%default, %specific);
Sign up to request clarification or add additional context in comments.

Comments

2

Well you could always just have tried it. If you pass a reference of the first hash you can store it like a nested structure.

use Data::Dumper;

%inner = (1, "monday", 2, "tuesday"); 
%outer = ("hello", 1, "days", \%inner);
print(Dumper(\%outer));
print($outer{'days'}{2});

OUTPUT

$VAR1 = {
          'hello' => 1,
          'days' => {
                      '2' => 'tuesday',
                      '1' => 'monday'
                    }
        };
tuesday

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.