I have a hash of arrays like this:
I know I can access the first element in one of the arrays like $g{k}[0]. But how do I get the length of that array?my %g = ( k => [8, 2, 10, 2, 1, 3], l => [10, 7, 9, 0, 1] );
Just try using random sigils, some combination is bound to work. The following script eases this task: it quickly runs all combinations of random sigils and prints the code that gives the right answer.
use 5.014; no warnings; # some non-sensical combination of sigils would give warn +ings use strict; # we want answers without symbolic references my %g = ( k => [8, 2, 10, 2, 1, 3], l => [10, 7, 9, 0, 1] ); sub sigil_combination { sprintf("%X", $_[0]) =~ y/0-9A-F/$@#%*~^\->(){}.,;/r; } for my $n0 (1..1e5) { my $c = "length " . sigil_combination($n0) . "g{k}"; if (6 == eval $c) { say "$c"; } } __END__
This prints the following standard solution using the array length operator $#:
and if you're lucky, it may print the following tricky solutions as a bonus too:length ~$~#g{k}
length {}%$g{k} length {}%@g{k} length {}-$g{k} length {}-@g{k}
In reply to Length of array in hash of arrays by ambrus
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |