- 论坛徽章:
- 0
|
回复
Hi,
SPLICING:
Hash has no original order by keys like an array. that means no first or last like an
array element pushed in a sequence.
So remove a key/value from a hash must be explicitly to say delete($hash{keyname}),
or use keys %hash and sort keys if possible, and you know that last key (sorted list) is
which you want to remove, then you can use splicing.
#############################################
SLICEING
Hash slice used to modify hash'svalues: format: @hash = @hash { KEY_LIST};
%tomAndJerry = (cat=>Tom, mouse=>Jerry);
# using hash slice to get values
@catAndMouse = @tomAndJerry{'cat', 'mouse'};
# @catAndMouse returns Tom, Jerry
# using hash slice to modifing hash value
@tomAndJerry{'cat', 'mouse'} = ('goofie', 'mikie');
# set value from key 'cat' as undef:
@tomAndJerry{'cat'} = (); # same as $tomAndJerry{'cat'} = undef;
# hash tomAndJerry remains still 2 keys.
# using delete to remove hash's key/value paar and hash's keys will reduced.
delete $tomAndJerry{'cat'};
# hash tomAndJerry contained only moues/Jerry.
regards, ulmer
[ 本帖最后由 ulmer 于 2006-10-16 23:36 编辑 ] |
|