如何传递“哈希数组”的所有元素作为数组放入函数中
如何将“哈希数组”的元素作为数组传递到函数中?
举例来说,我想将所有 $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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果您想按文本对链接进行排序,
如果您实际上只想获取文本并对其进行排序,
If you want to sort your links by text,
If you actually just want to get and sort the text,
最好谨慎一点,使用 HTML 解析器来解析 HTML:
Better to err on the side of caution and use an HTML parser to parse HTML: