开发者

Hash set in Perl

Consider the following code:

$inFilesToCopy{$filename} = $filename;

I have a hash table where both the key and value are the name of a file. I would like to avoid using开发者_开发百科 extra memory and not store the filename twice.

Is there a set object in Perl?


Set::Object works as you'd expect:

use Set::Object qw(set);

my $files = set();
$files->insert($file);
my @files = $files->members();

You can also do set math:

my $a = set();
$a->insert(...);

my $b = set();
$b->insert(...);

my $only_in_a = $a - $b;
copy_to_b($only_in_a->members);


You might consider doing:

$inFilesToCopy{$_} = 1;

and then you can do tests like this:

if($inFilesToCopy{$filename}) { ... }

you can also use 'keys %inFilesToCopy' to get the list of files.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜