2 kya yan kya_yan 于 2017.09.17 22:17 提问

perl 数组移除一个hash,返回一个新数组 5C
 my @products = (
            {"value"=> "apple", "mount"=>"1"},
            {"value"=> "bag", "mount"=>"2"},
            {"value"=> "car", "mount"=>"3"}
 );

移除value为bag的hash,返回新的数组

my @newproducts = (
                {"value"=> "apple", "mount"=>"1"},
                {"value"=> "car", "mount"=>"3"}
);

1个回答

u012369580
u012369580   2017.09.21 17:28

#! /usr/bin/perl -w
use Data::Dumper;
my @products = (
{"value"=> "apple", "mount"=>"1"},
{"value"=> "bag", "mount"=>"2"},
{"value"=> "car", "mount"=>"3"}
);
my @newproducts = ();
foreach my $tmp (@products) {
next if (grep (/^bag$/,values %{$tmp}));
push @newproducts,$tmp;
}
print Dumper(\@products);
print Dumper(\@newproducts);

亲测可用

Csdn user default icon
上传中...
上传图片
插入图片