ilyas patanam
ilyas patanam

Reputation: 5324

Find maximum index of an array nested in a hash in Perl

I have a hash with two keys and the values are in an array. so,

%graph;
@{$graph{$root}{"children"} = ('apple', 'banana', 'orange');

I am trying to get the maximal size of the index which is 2, usually I would do

$#array

However, when I do

$#{$graph{$root}{"children"}

it gets commented out.

Upvotes: 0

Views: 441

Answers (1)

Jonathan Leffler
Jonathan Leffler

Reputation: 754110

The code below works for me, giving the output:

$ perl x.pl
List: apple banana orange
Size: 2
$

Code — note the properly closed sets of braces (the code in the question has issues that prevent it compiling):

#!/usr/bin/env perl
use strict;
use warnings;
use English qw( -no_match_vars );

my $root = "root";

$OFS = " ";
my %graph;
@{$graph{$root}{"children"}} = ('apple', 'banana', 'orange');

print  "List:", @{$graph{$root}{"children"}}, "\n";
printf "Size: %d\n", $#{$graph{$root}{"children"}};

(Perl 5.12.1 on RHEL 5 for x86/64)

Upvotes: 3

Related Questions