hel*_*mal 3 perl hash perl-data-structures
my %hash1 = (
a=>192.168.0.1,
b=>192.168.0.1,
c=>192.168.2.2,
d=>192.168.2.3,
e=>192.168.3.4,
f=>192.168.3.4
);
Run Code Online (Sandbox Code Playgroud)
我有一个像上面给出的perl哈希.密钥是设备名称,值是IP地址.如何使用%hash1创建没有重复IP地址(如%hash2)的哈希?(删除了具有相同ips的设备)
my %hash2 = ( c=>192.168.2.2, d=>192.168.2.3 );
Run Code Online (Sandbox Code Playgroud)
首先,你需要引用你的IP地址,因为192.168.0.1perl中的V-String是指chr(192).chr(168).chr(0).chr(1).
而我的变体是:
my %t;
$t{$_}++ for values %hash1; #count values
my @keys = grep
{ $t{ $hash1{ $_ } } == 1 }
keys %hash1; #find keys for slice
my %hash2;
@hash2{ @keys } = @hash1{ @keys }; #hash slice
Run Code Online (Sandbox Code Playgroud)