The following module was proposed for inclusion in the Module List:
modid: Tie::Silk
DSLIP: bdprp
description: Indexed hash prepend w/ `-' to get index/key
userid: DIRT (Theo Lengyel)
chapterid: 6 (Data_Type_Utilities)
communities:
similar:
Tie::IxHash
rationale:
Tie::Silk - Silk = Somewhat Intuitive List Knitter
This is an indexed hash with some added strange behavior. It is
useful for keeping track of several related lists based on the same
key set. When an (existing) key is prepended with a `-' (minus sign
- regular keys may NOT begin with a `-') it returns the index for
that element. When an index (integer) is used to access the hash it
acts like an array (new values may NOT be introduced with integer
keys) and returns/sets the VALUE in that slot. When an integer is
prepended with a `-' it returns/sets the corresponding KEY for that
slot. When accessed with normal text keys is behaves like a normal
hash, except that order is preserved.
# this is a hash that also acts like a funny array. It preserves
order # like an array(akin to Tie::IxHash), but also has some other
specific # behavior that is described below ( the `===' I am using
here to mean # `equivalent to') # where $x is an integer $x >= 0 #
then $h{$x} === $h{$h{"-$x"}} # # here is a sample hash: # $th = tie
%h, __PACKAGE__ # %h = qw( _foo bar # _baz fup # ); #
Taowebs::Tie::Silk; these are the behaviors we are after: # 1) when
accessed with a negative number it returns the hash hey for # the
indicated pair(need to be careful with a `minus zero' key # MUST be
a string because perl compiler will convert an unquoted -0 to 0) #
$h{'-0'} === _foo # $h{-1} === _baz # 2) when accessed with a
positive integer it returns the value for # the associated key pair
(same as fetching it with the normal # hash key, but now we are
accessing it with pseudo-array indexing # using {} instead of []
notation) # $h{0} === bar === $h{_foo} === $h{$h{'-0'}} # $h{1} ===
fup === $h{_baz} === $h{$h{-1}} # $h{_baz} === fup # $h{_foo} ===
bar # 3) a normal hash key preceded by a `-' (minus sign) returns
the # index of that pair ( except when setting a value this way, see
#4) # $h{-_baz} === 1 # $h{-_foo} === 0 # 4) when setting an element
and preceding the hash key with a minus # sign `-' this actually
replaces the hash key with the value # therefore we have: #
$h{-_foo} = '_new_foo' === $h{'-0'} # equivalently: # $h{'-0'} =
'_new_foo' === $h{'-0'} # $h{_new_foo} === bar # $h{-_new_foo} === 0
# $h{'-0'} === _new_foo # 5) you cannot set a new slot with an
integer unless the integer matches the next open slot, otherwise
confusion may ensue #
enteredby: DIRT (Theo Lengyel)
enteredon: Thu Nov 6 19:38:15 2003 GMT
The resulting entry would be:
Tie::
::Silk bdprp Indexed hash prepend w/ `-' to get index/key DIRT
Thanks for registering,
--
The PAUSE
PS: The following links are only valid for module list maintainers:
Registration form with editing capabilities:
https://pause.perl.org/pause/authenquery?ACTION=add_mod&USERID=73500000_95e73362a06ea6fb&SUBMIT_pause99_add_mod_preview=1
Immediate (one click) registration:
https://pause.perl.org/pause/authenquery?ACTION=add_mod&USERID=73500000_95e73362a06ea6fb&SUBMIT_pause99_add_mod_insertit=1