哈希表:有效计数与钥匙匹配的哈希

发布于 2025-02-13 16:21:38 字数 2620 浏览 0 评论 0原文

我想检查perl中的IP -J添加ENO1的JSON输出。 我想计算NIC拥有多少IPv4。我仍然需要IPv6信息,因此我想使用-4,然后是-6标志的Avoir两次/usr/bin/ip命令。

现在,我可以访问这样的IP信息: $ nic-> {addr_info} [0] - > {local} 对于每个条目,$ nic-> {addr_info} [$ ip_info_index] - > {family}

在我表中的大多数情况下 将提供IP类型(INET或INET6)对于V4,一个用于V6,但有时我有2个V4条目A,并且想在我的软件中发布警告“不支持”。

$ nic--> {addr_info} [$ ip_info_index] - > {family}将提供条目的类型。

是否有使用MAP和标量来计算多少$ nic-> {addr_info} [$ ip_info_index] - > {family}等于'inet'(而不是'inet6' )

(我可以在$ ip_info_index上循环,并在每次看到“ Inet”时增加计数器,但这似乎并不优雅)。

  DB<3> p Dumper($nic)
$VAR1 = {
          'txqlen' => 1000,
          'address' => '00:26:b9:7d:c0:ee',
          'broadcast' => 'ff:ff:ff:ff:ff:ff',
          'link_type' => 'ether',
          'group' => 'default',
          'mtu' => 1500,
          'qdisc' => 'mq',
          'flags' => [
                       'BROADCAST',
                       'MULTICAST',
                       'UP',
                       'LOWER_UP'
                     ],
          'operstate' => 'UP',
          'ifindex' => 2,
          'addr_info' => [
                           {
                             'valid_life_time' => 30949,
                             'preferred_life_time' => 30949,
                             'label' => 'eno1',
                             'family' => 'inet',
                             'scope' => 'global',
                             'noprefixroute' => bless( do{\(my $o = 1)}, 'JSON::PP::Boolean' ),
                             'prefixlen' => 24,
                             'local' => '172.16.59.72',
                             'broadcast' => '172.16.59.255',
                             'dynamic' => $VAR1->{'addr_info'}[0]{'noprefixroute'}
                           },
                           {
                             'family' => 'inet6',
                             'local' => 'fe80::226:b9ff:fe7d:c0ee',
                             'valid_life_time' => 4294967295,
                             'preferred_life_time' => 4294967295,
                             'prefixlen' => 64,
                             'scope' => 'link'
                           }
                         ],
          'ifname' => 'eno1'
        };

对于addr_info表,我希望计算有多少哈希具有'family'=&gt; “ Inet”(对于“ Inet6”)。 如果设置一个IPv4或一个IPv6,我需要失败。 如果您想在Linux系统上进行测试,则可以获得$ NIC:

my $ip_addr_output = `LC_ALL=C /sbin/ip -j addr 2>/dev/null`;

I want to check the json output of ip -j adds show eno1 in perl.
I want to count how many ipv4 addr the Nic has. I still need the ipv6 info so I want to avoir running twice /usr/bin/ip command with the -4 and then -6 flag.

For now I can access to the ip information like this:
$nic->{addr_info}[0]->{local}
For each entry, the $nic->{addr_info}[$ip_info_index]->{family} will give the IP type (inet or inet6)

In most cases 2 entries in my table: one for v4 and one for v6, but sometimes I have 2 v4 entries a and want to issue a warning "not supported" in my software.

$nic->{addr_info}[$ip_info_index]->{family} will give the type of entry.

Is there some elegant trick using map and scalar to count how many $nic->{addr_info}[$ip_info_index]->{family} are equal to 'inet' (and not 'inet6')

(I can loop over $ip_info_index , and increment a counter each time I see 'inet', but that seems not elegant).

  DB<3> p Dumper($nic)
$VAR1 = {
          'txqlen' => 1000,
          'address' => '00:26:b9:7d:c0:ee',
          'broadcast' => 'ff:ff:ff:ff:ff:ff',
          'link_type' => 'ether',
          'group' => 'default',
          'mtu' => 1500,
          'qdisc' => 'mq',
          'flags' => [
                       'BROADCAST',
                       'MULTICAST',
                       'UP',
                       'LOWER_UP'
                     ],
          'operstate' => 'UP',
          'ifindex' => 2,
          'addr_info' => [
                           {
                             'valid_life_time' => 30949,
                             'preferred_life_time' => 30949,
                             'label' => 'eno1',
                             'family' => 'inet',
                             'scope' => 'global',
                             'noprefixroute' => bless( do{\(my $o = 1)}, 'JSON::PP::Boolean' ),
                             'prefixlen' => 24,
                             'local' => '172.16.59.72',
                             'broadcast' => '172.16.59.255',
                             'dynamic' => $VAR1->{'addr_info'}[0]{'noprefixroute'}
                           },
                           {
                             'family' => 'inet6',
                             'local' => 'fe80::226:b9ff:fe7d:c0ee',
                             'valid_life_time' => 4294967295,
                             'preferred_life_time' => 4294967295,
                             'prefixlen' => 64,
                             'scope' => 'link'
                           }
                         ],
          'ifname' => 'eno1'
        };

for addr_info table I wantt to count how many hashes have 'family' => 'inet' (and same for 'inet6').
I need to fail if more that one ipv4 or one ipv6 is set.
if you want to test on a linux system, the $nic is obtained like this:

my $ip_addr_output = `LC_ALL=C /sbin/ip -j addr 2>/dev/null`;

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

就是爱搞怪 2025-02-20 16:21:39

我认为这就是您想要的。

$ NIC-&GT; {ADDR_INFO}是对数组的引用,其中每个元素描述了连接到接口的IP地址之一。

因此,@{$ nic-&gt; {addr_info}} dereferences数组,因此您现在可以将其传递给需要数组或列表的函数。一个这样的功能是 grepefterters efterts efterts 仅返回列表在满足某些标准的列表中。因此,我们可以使用:

grep { $_->{family} eq 'inet' } @{ $nic->{addr_info} }

如果您在标量上下文中调用`grep,它不会给您列表,它为您提供列表中的项目数量。

scalar grep { $_->{family} eq 'inet' } @{ $nic->{addr_info} }

因此,您可以使用类似的东西:

say "$nic->{ifname} : ", scalar grep { $_->{family} eq 'inet' } @{ $nic->{addr_info} };

I think this is what you want.

$nic->{addr_info} is a reference to an array where each element describes one of the IP addresses attached to the interface.

So @{ $nic->{addr_info} } dereferences that array so you can now pass it to functions that require arrays or lists. One such function is grep which filters a list and only returns elements in the list which satisfy some criteria. We can therefore get a list of IPv4 addresses using:

grep { $_->{family} eq 'inet' } @{ $nic->{addr_info} }

If you call `grep in scalar context, it doesn't give you the list, it gives you the number of items in the list.

scalar grep { $_->{family} eq 'inet' } @{ $nic->{addr_info} }

So you can use something like this:

say "$nic->{ifname} : ", scalar grep { $_->{family} eq 'inet' } @{ $nic->{addr_info} };
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文