如何传递“哈希数组”的所有元素作为数组放入函数中

发布于 2024-10-08 00:30:37 字数 694 浏览 4 评论 0原文

如何将“哈希数组”的元素作为数组传递到函数中?

举例来说,我想将所有 $link->{text} 作为数组传递到 sort() 函数中。

#!/usr/bin/perl
use strict; use warnings;

my $field = <<EOS;
<a href="baboon.html">Baboon</a>
<a href="antelope.html">Antelope</a>
<a href="dog.html">dog</a>
<a href="cat.html">cat</a>
EOS

#/ this comment is to unconfuse the SO syntax highlighter. 
my @array_of_links;
while ($field =~ m{<a.*?href="(.*?)".*?>(.*?)</a>}g) {
    push @array_of_links, { url => $1, text => $2 };
}
for my $link (@array_of_links) {
    print qq("$link->{text}" goes to -> "$link->{url}"\n);
}

How do I pass a element of "array of hashes" into function as an array?

say for instance I wanted to pass all $link->{text} as an array into the sort() function.

#!/usr/bin/perl
use strict; use warnings;

my $field = <<EOS;
<a href="baboon.html">Baboon</a>
<a href="antelope.html">Antelope</a>
<a href="dog.html">dog</a>
<a href="cat.html">cat</a>
EOS

#/ this comment is to unconfuse the SO syntax highlighter. 
my @array_of_links;
while ($field =~ m{<a.*?href="(.*?)".*?>(.*?)</a>}g) {
    push @array_of_links, { url => $1, text => $2 };
}
for my $link (@array_of_links) {
    print qq("$link->{text}" goes to -> "$link->{url}"\n);
}

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

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

发布评论

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

评论(2

绝情姑娘 2024-10-15 00:30:37

如果您想按文本对链接进行排序,

my @sorted_links = sort { $a->{text} cmp $b->{text} } @array_of_links;

如果您实际上只想获取文本并对其进行排序,

my @text = sort map $_->{text}, @array_of_links;

If you want to sort your links by text,

my @sorted_links = sort { $a->{text} cmp $b->{text} } @array_of_links;

If you actually just want to get and sort the text,

my @text = sort map $_->{text}, @array_of_links;
微暖i 2024-10-15 00:30:37

最好谨慎一点,使用 HTML 解析器来解析 HTML:

use strict; use warnings;

use HTML::TokeParser::Simple;

my $field = <<EOS;
<a href="baboon.html">Baboon</a>
<a href="antelope.html">Antelope</a>
<a href="dog.html">dog</a>
<a href="cat.html">cat</a>
EOS

my $parser = HTML::TokeParser::Simple->new(string => $field);

my @urls;

while ( my $tag = $parser->get_tag ) {
    next unless $tag->is_start_tag('a');
    next unless defined(my $url = $tag->get_attr('href'));
    my $text = $parser->get_text('/a');
    push @urls, { url => $url, text => $text };
}

@urls = sort {
    $a->{text} cmp $b->{text} ||
    $a->{url}  cmp $b->{url}
} @urls;

use YAML;
print Dump \@urls;

Better to err on the side of caution and use an HTML parser to parse HTML:

use strict; use warnings;

use HTML::TokeParser::Simple;

my $field = <<EOS;
<a href="baboon.html">Baboon</a>
<a href="antelope.html">Antelope</a>
<a href="dog.html">dog</a>
<a href="cat.html">cat</a>
EOS

my $parser = HTML::TokeParser::Simple->new(string => $field);

my @urls;

while ( my $tag = $parser->get_tag ) {
    next unless $tag->is_start_tag('a');
    next unless defined(my $url = $tag->get_attr('href'));
    my $text = $parser->get_text('/a');
    push @urls, { url => $url, text => $text };
}

@urls = sort {
    $a->{text} cmp $b->{text} ||
    $a->{url}  cmp $b->{url}
} @urls;

use YAML;
print Dump \@urls;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文