我想问一下如何在附加到数组之前进行验证(停止附加重复元素)?
sub create_release_text_file {
my $result_path = shift;
my %hshReleasebuild;
my $json_releasebuild_array;
my $json_releasebuild_text;
my $json = JSON->new->allow_nonref;
$hshReleasebuild{"Build"} = $ARG_BUILD;
$hshReleasebuild{"Release"} = $ARG_RELEASE;
my $release_path = File::Spec->catfile($result_path, "release.txt");
if (-e $release_path) {
open RELEASE_FILE, "<", $release_path or die $!;
my $json = do { local $/; <RELEASE_FILE> };
$json_releasebuild_array = decode_json($json);
close RELEASE_FILE;
# print Dumper(\$json_releasebuild_array);
}
# Do the validation here before appending into the array
push(@{$json_releasebuild_array}, \%hshReleasebuild);
$json_releasebuild_text = $json->encode(\@{$json_releasebuild_array});
open RELEASE_FILE, ">", $release_path or die $!;
print RELEASE_FILE $json_releasebuild_text;
close RELEASE_FILE;
答案 0 :(得分:0)
所以我会为精简列表创建一个新数组,并使用哈希来跟踪你所看到的内容:
my ( @trimlist, %trimkeys ) ;
for ( @$json_releasebuild_array )
{
my $kval= "r$_->{Release}b$_->{Build}" ; # make a unique string for each entry
next if exists $trimkeys{$kval} ;
push @trimlist, $_ ;
$trimkeys{$kval}= 1 ;
}
$json_releasebuild_array= \@trimlist ;