从傻瓜中提取信息

发布于 2025-01-22 11:26:30 字数 1398 浏览 2 评论 0原文

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

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

发布评论

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

评论(1

一笑百媚生 2025-01-29 11:26:30

看来您有数据结构列表,您将传递给Data :: Dumper。

my @all_results = (
    {
        'fruit'    => 'apple',
        'uniqueID' => 'red'
    },
    {
        'fruit'    => 'apple',
        'uniqueID' => 'green',
    },
);

print Dumper @all_results;

这将用$ var1$ var2产生输出。

现在,如果要搜索@all_results中的数据结构,其中key unique is “ green”,您可以使用 grep 查看每个数据结构,并仅过滤您想要的一个。

( my $filtered ) = grep { $_->{'uniqueID'} eq 'green' } @all_results;
print Dumper $filtered;

请注意,您需要括号()围绕新变量,因为grep返回列表。您需要在列表上下文中分配,否则您将获得结果的元素数量。 (在这种情况下是1)。

以上代码的输出是

$VAR1 = {
          'uniqueID' => 'green',
          'fruit' => 'apple'
        };

It looks like you have a list of data structures, that you are passing to Data::Dumper.

my @all_results = (
    {
        'fruit'    => 'apple',
        'uniqueID' => 'red'
    },
    {
        'fruit'    => 'apple',
        'uniqueID' => 'green',
    },
);

print Dumper @all_results;

That would produce the output with $VAR1 and $VAR2 you've shows in the question.

Now if you want to search for the data structure inside @all_results where the key uniqueID is "green", you can use grep to look into every data structure, and filter out only the one you want.

( my $filtered ) = grep { $_->{'uniqueID'} eq 'green' } @all_results;
print Dumper $filtered;

Note that you need the parentheses () around the new variable, as grep returns a list. You need to assign in list context, otherwise you'll get the number of elements of the result back. (That's 1 in this case).

The output of above code is

$VAR1 = {
          'uniqueID' => 'green',
          'fruit' => 'apple'
        };
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文