免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
12下一页
最近访问板块 发新帖
查看: 2997 | 回复: 10
打印 上一主题 下一主题

HASH可以splice么? [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2006-10-16 20:14 |只看该作者 |倒序浏览
我们知道splice删除数组一个eliment的时候,可以将eliment占用内存释放,而delete却不可以,那么有什么办法对hash数组作splice么?

除了先将hash转换为array的方法外!

另外我只想splice一个eliment,而不想清空整个hash数组

论坛徽章:
0
2 [报告]
发表于 2006-10-16 22:35 |只看该作者
Hash的数据结构需要使用splice delete吗?

论坛徽章:
0
3 [报告]
发表于 2006-10-16 23:05 |只看该作者

回复

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 编辑 ]

论坛徽章:
0
4 [报告]
发表于 2006-10-17 00:19 |只看该作者
i think the func delete  will not remove the pair of key and value, a slot will be left when delete a element!

谁知道怎么去跟踪perl使用内存的情况呢?

论坛徽章:
0
5 [报告]
发表于 2006-10-17 00:35 |只看该作者
原帖由 bulletming 于 2006-10-17 00:19 发表
i think the func delete  will not remove the pair of key and value, a slot will be left when delete a element!

谁知道怎么去跟踪perl使用内存的情况呢?


Why not?! if say delete $tomAndJerry{'cat'}, what's happend %tomAndJerry?
If say $tomAndJerry{'cat'} = undef, what's happend %tomAndJerry?

try this:
use Data::Dumper;
%disney = ('cat'=>'Tom', 'mouse' => 'Jerry', dog=>'goofie');
delete $disney{'cat'};
$disney{'dog'} = '';
print Dumper(\%disney);

论坛徽章:
0
6 [报告]
发表于 2006-10-17 09:48 |只看该作者
  1. #! /usr/local/bin/perl

  2. my %test = (aaa => '123',bbb => '234',ccc=> '345');

  3. print \$test{aaa}." key is aaa value is ".$test{aaa}."\n";
  4. print \$test{bbb}." key is bbb value is ".$test{bbb}."\n";
  5. print \$test{ccc}." key is ccc value is ".$test{ccc}."\n";

  6. delete($test{bbb});

  7. print \$test{aaa}." key is aaa value is ".$test{aaa}."\n";
  8. print \$test{ccc}." key is ccc value is ".$test{ccc}."\n";

  9. $test{ddd} = '999';

  10. print \$test{aaa}." key is aaa value is ".$test{aaa}."\n";
  11. print \$test{bbb}." key is bbb value is ".$test{bbb}."\n";
  12. print \$test{ccc}." key is ccc value is ".$test{ccc}."\n";
  13. print \$test{ddd}." key is ddd value is ".$test{ddd}."\n";
复制代码


结果:
root@ldapm # ./spli_hash.pl
SCALAR(0x1163f0) key is aaa value is 123
SCALAR(0x116504) key is bbb value is 234
SCALAR(0x116d20) key is ccc value is 345
SCALAR(0x1163f0) key is aaa value is 123
SCALAR(0x116d20) key is ccc value is 345
SCALAR(0x1163f0) key is aaa value is 123
SCALAR(0x11630c) key is bbb value is
SCALAR(0x116d20) key is ccc value is 345
SCALAR(0x116504) key is ddd value is 999

上边例子中说明了delete bbb之后,再增加一个ddd的key,它使用了原先bbb的内存空间。
我不知道是否可以说明这个问题!

等会,我作一个非常大的hash数组,看delete之后,是否prstat看到的内存会减少!

论坛徽章:
0
7 [报告]
发表于 2006-10-17 09:48 |只看该作者
谢谢! ulmer!

Data:umper很好用!

论坛徽章:
1
2015年辞旧岁徽章
日期:2015-03-03 16:54:15
8 [报告]
发表于 2006-10-17 10:01 |只看该作者
Perl 的 hash 空间是自动维护的,这没有什么好说的。

论坛徽章:
0
9 [报告]
发表于 2006-10-18 17:19 |只看该作者
undef可以吗?

论坛徽章:
0
10 [报告]
发表于 2006-10-19 11:38 |只看该作者
use Data:umper;
%disney = ('cat'=>'Tom', 'mouse' => 'Jerry', dog=>'goofie');
delete $disney{'cat'};
undef $disney{'dog'};
print Dumper(\%disney);
显示的结果为:
$VAR1 = {
          'dog' => undef,
          'mouse' => 'Jerry'
        };
不知道是否为楼上的所想要的结果!
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP