Question
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] );
Answer
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}
|
---|
Replies are listed 'Best First'. | |
---|---|
Re: Length of array in hash of arrays
by Grimy (Pilgrim) on May 12, 2013 at 08:43 UTC | |
by ambrus (Abbot) on May 12, 2013 at 08:50 UTC | |
by Anonymous Monk on Jul 05, 2013 at 16:58 UTC | |
by gurpreetsingh13 (Scribe) on May 13, 2013 at 05:29 UTC | |
by ambrus (Abbot) on Oct 24, 2013 at 15:27 UTC |
Back to
Obfuscated Code