AskOverflow.Dev

AskOverflow.Dev Logo AskOverflow.Dev Logo

AskOverflow.Dev Navigation

  • 主页
  • 系统&网络
  • Ubuntu
  • Unix
  • DBA
  • Computer
  • Coding
  • LangChain

Mobile menu

Close
  • 主页
  • 系统&网络
    • 最新
    • 热门
    • 标签
  • Ubuntu
    • 最新
    • 热门
    • 标签
  • Unix
    • 最新
    • 标签
  • DBA
    • 最新
    • 标签
  • Computer
    • 最新
    • 标签
  • Coding
    • 最新
    • 标签
主页 / unix / 问题 / 557622
Accepted
BugBuddy
BugBuddy
Asked: 2019-12-17 20:34:56 +0800 CST2019-12-17 20:34:56 +0800 CST 2019-12-17 20:34:56 +0800 CST

如何在哈希而不是数组上使用 Perl grep?

  • 772

我正在学习 Perl。我已经能够grep在数组上使用并且语法非常简单,比如这个例子:

use strict; use warnings;

my @names = qw(Foo Bar Baz);
my $visitor = <STDIN>;
chomp $visitor;
if (grep { $visitor eq $_ } @names) {
   print "Visitor $visitor is in the guest list\n";
} else {
   print "Visitor $visitor is NOT in the guest list\n";
}

但是,我想知道是否有一种同样简单的方法可以grep在 hash 上使用而无需编写循环来遍历 hash 中的每个项目。

这是我正在使用的结构的一些示例数据。在分配 URI 之前,我想检查是否有任何项目已经具有该 uri 值。例如,我想分配ww1.example.com给 item v2rbz1568,但前提是没有其他项目的 uri 值为ww1.example.com。我怎样才能在 Perl 中有效地做到这一点?

{
    "0y7vfr1234": {
        "username": "[email protected]",
        "password": "some-random-password123",
        "uri": "ww1.example.com",
        "index": 14
    },
    "v2rbz1568": {
        "username": "[email protected]",
        "password": "some-random-password125",
        "uri": "ww3.example.com",
        "index": 29
    },
    "0zjk1156": {
        "username": "[email protected]",
        "password": "some-random-password124",
        "uri": "ww2.example.com",
        "index": 38
    }
}

我在 Linux 上使用 perl 5,版本 30。

perl
  • 1 1 个回答
  • 3199 Views

1 个回答

  • Voted
  1. Best Answer
    Jonas Berlin
    2019-12-17T21:21:20+08:002019-12-17T21:21:20+08:00

    至少有两种选择:

    1. 您(仅)拥有您在问题中设想的数据结构。然后,每次要查找匹配项时,您都必须遍历整个“列表”。您不必编写循环,您可以使用该map函数:
    use strict; use warnings;
    my %entries = (
        '0y7vfr1234' => {
            'username' => '[email protected]',
            'password' => 'some-random-password123',
            'uri' => 'ww1.example.com',
            'index' => 14
        },
        'v2rbz1568' => {
            'username' => '[email protected]',
            'password' => 'some-random-password125',
            'uri' => 'ww3.example.com',
            'index' => 29
        }
    );
    my $uri = <STDIN>;
    chomp $uri;
    if (grep { $uri eq $_ } map { $_->{'uri'} } values %entries) {
       print "URI $uri is in the list\n";
    } else {
       print "URI $uri is NOT in the list\n";
    }
    
    1. 您在散列中管理一个单独的索引,以便您可以进行快速查找。索引意味着您有一个单独的哈希映射 URI 到实际哈希的项目:
    use strict; use warnings;
    my %entries = (
        '0y7vfr1234' => {
            'username' => '[email protected]',
            'password' => 'some-random-password123',
            'uri' => 'ww1.example.com',
            'index' => 14
        },
        'v2rbz1568' => {
            'username' => '[email protected]',
            'password' => 'some-random-password125',
            'uri' => 'ww3.example.com',
            'index' => 29
        }
    );
    my %index = map { $entries{"uri"} => $_ } keys %entries;
    
    my $uri = <STDIN>;
    chomp $uri;
    my $item = $index{$uri};
    if (defined($item)) {
       print "URI $uri is in the list\n";
    } else {
       print "URI $uri is NOT in the list\n";
    }
    

    这也很方便,因为您可以$item直接作为查找的结果获得print "Index: ",$entries{$item}->{'index'},"\n";

    在第二种情况下,每次在“列表”中添加/更新/删除 URI 时,您都必须手动更新索引:

    $entries{"v84x9v8b9"} = { uri => "ww49", ... };
    $index{"ww49"} = "v84x9v8b9";
    
    • 2

相关问题

  • zabbix_sender 错误

  • cpanm 与 apt 升级

  • 匹配 CSV 文件中的可逆对

  • Perl:可以使用变量替换吗?

  • 如果总和小于特定值,则计算每 2 行的总和并用另一个值替换它们

Sidebar

Stats

  • 问题 205573
  • 回答 270741
  • 最佳答案 135370
  • 用户 68524
  • 热门
  • 回答
  • Marko Smith

    模块 i915 可能缺少固件 /lib/firmware/i915/*

    • 3 个回答
  • Marko Smith

    无法获取 jessie backports 存储库

    • 4 个回答
  • Marko Smith

    如何将 GPG 私钥和公钥导出到文件

    • 4 个回答
  • Marko Smith

    我们如何运行存储在变量中的命令?

    • 5 个回答
  • Marko Smith

    如何配置 systemd-resolved 和 systemd-networkd 以使用本地 DNS 服务器来解析本地域和远程 DNS 服务器来解析远程域?

    • 3 个回答
  • Marko Smith

    dist-upgrade 后 Kali Linux 中的 apt-get update 错误 [重复]

    • 2 个回答
  • Marko Smith

    如何从 systemctl 服务日志中查看最新的 x 行

    • 5 个回答
  • Marko Smith

    Nano - 跳转到文件末尾

    • 8 个回答
  • Marko Smith

    grub 错误:你需要先加载内核

    • 4 个回答
  • Marko Smith

    如何下载软件包而不是使用 apt-get 命令安装它?

    • 7 个回答
  • Martin Hope
    user12345 无法获取 jessie backports 存储库 2019-03-27 04:39:28 +0800 CST
  • Martin Hope
    Carl 为什么大多数 systemd 示例都包含 WantedBy=multi-user.target? 2019-03-15 11:49:25 +0800 CST
  • Martin Hope
    rocky 如何将 GPG 私钥和公钥导出到文件 2018-11-16 05:36:15 +0800 CST
  • Martin Hope
    Evan Carroll systemctl 状态显示:“状态:降级” 2018-06-03 18:48:17 +0800 CST
  • Martin Hope
    Tim 我们如何运行存储在变量中的命令? 2018-05-21 04:46:29 +0800 CST
  • Martin Hope
    Ankur S 为什么 /dev/null 是一个文件?为什么它的功能不作为一个简单的程序来实现? 2018-04-17 07:28:04 +0800 CST
  • Martin Hope
    user3191334 如何从 systemctl 服务日志中查看最新的 x 行 2018-02-07 00:14:16 +0800 CST
  • Martin Hope
    Marko Pacak Nano - 跳转到文件末尾 2018-02-01 01:53:03 +0800 CST
  • Martin Hope
    Kidburla 为什么真假这么大? 2018-01-26 12:14:47 +0800 CST
  • Martin Hope
    Christos Baziotis 在一个巨大的(70GB)、一行、文本文件中替换字符串 2017-12-30 06:58:33 +0800 CST

热门标签

linux bash debian shell-script text-processing ubuntu centos shell awk ssh

Explore

  • 主页
  • 问题
    • 最新
    • 热门
  • 标签
  • 帮助

Footer

AskOverflow.Dev

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

Language

  • Pt
  • Server
  • Unix

© 2023 AskOverflow.DEV All Rights Reserve