for performance reasons¹ I have to hold data in an array instead of a hash, but I don't wanna obfuscate my code with numerical indices.
IIRC there is a deprecated approach to define hash-like arrays for this reason (actually I can't find it in the 5.10 docs anymore)
The best solution I came up till now is using constants in a dedicated package to hold the indices:
my @arr=("jesus","heaven"); { package t; sub NAME () {0}; sub ADDRESS () {1}; print $arr[NAME],$arr[ADDRESS] } print $arr[t::NAME],$arr[t::ADDRESS];
Such that the indices will be expanded at compile time.
> perl -MO=Deparse /tmp/tst.pl sub t::NAME () { 0 } sub t::ADDRESS () { 1 } my(@arr) = ('jesus', 'heaven'); { package t; print $arr[0], $arr[1]; } print $arr[0], $arr[1]; /tmp/tst.pl syntax OK
Any other suggestions???
Cheers Rolf
UPDATE: hmm I could use ' instead of :: to shorten the notation.
1) a deeply branching recursive function.
In reply to Named array indices by LanX
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |