如何在perl中删除哈希值中的重复值?

问题描述 投票:-3回答:1

我有一个300行的文本文件,其中100行重复。我不知道如何搜索我的哈希并删除这些重复。

当我必须添加新的代码行时,这是我的perl代码:

perl hash delete-row
1个回答
0
投票

您可以简单地使用uniq()模块中的List::MoreUtils在数组中包含单个项目,并且最好在将哈希值放入哈希值之前删除重复的项目

类似这样的事情会做到:

use strict;
use warnings;
use Data::Dumper;
use List::MoreUtils qw(uniq);

my %cathmin;

open (LIST, "<cath_min.txt") || die "[-] Can't open the file";
my @list = <LIST>; # load your array from your file
close LIST;

@list = uniq(@list); # remove duplicated items in array

foreach my $item (@list) {
  chomp $item;
  # here you can do what you want
}

print Dumper(\%cathmin); # check your hash contents
© www.soinside.com 2019 - 2024. All rights reserved.