小编Men*_*hem的帖子

为什么添加或删除换行符会改变perl for for循环函数的方式?

我开始学习perl,使用perl.org上提供Wrox Beginning Perl,并对第3章中提供的for循环示例提出疑问.

#!/usr/bin/perl

use warnings;
use strict;

my @count = (1..10);
for (reverse(@count)) {
        print "$_...\n";
        sleep 1;
}
print "Blast Off!\n"
Run Code Online (Sandbox Code Playgroud)

这是他们提供的脚本,它按预期工作.它显示一个数字,然后是......每秒钟,在每个数字之间等待一秒钟.完成后,显示Blast Off!

但是,如果我从print语句中删除换行符,则行为会发生变化.该脚本静默等待10秒钟,然后立即显示所有10个数字Blash Off!.为什么要改变?

#!/usr/bin/perl

use warnings;
use strict;

my @count = (1..10);
for (reverse(@count)) {
        print "$_...";
        sleep 1;
}
print "Blast Off!\n"
Run Code Online (Sandbox Code Playgroud)

perl for-loop newline

3
推荐指数
1
解决办法
639
查看次数

是否可以使用bash访问for循环中的多个数组

我正在尝试编写一个bash脚本,让我可以使用curl下载多个网页.对于每个网页,我希望能够传递curl页面和referer链接.我希望能够一次提供多个网页.

换句话说,我希望能够遍历我提供脚本的网页,并且对于每个页面,将关联的网页和引用链接传递给curl.

我以为我会使用数组将网页和referer链接存储在一个变量中,以为我可以在运行curl时提取数组的各个元素.

我的问题是我无法弄清楚如何让多个数组在for循环中正常工作.这是我想要做的事情的想法.此代码不起作用,因为"$ i"(在for循环中)不会成为数组.

#every array has the information for a separate webpage
array=( "webpage" "referer" )
array2=( "another webpage" "another referer" )

for i in "${array[@]}" "${array2[@]}" #line up multiple web pages
do
    #use curl to download the page, giving the referer ("-e")
    curl -O -e "${i[1]}" "${i[0]}"
done
Run Code Online (Sandbox Code Playgroud)

如果我只使用一个数组,我可以轻松地这样做:

array=( "webpage" "referer" )
REFERER="${array[1]}"
PAGE="${array[0]}"
#use curl to download the page, giving the referer ("-e")
curl -O -e "$REFERER" "$LINK"
Run Code Online (Sandbox Code Playgroud)

曾经有一个我想要一次处理的网页,我无法弄清楚如何正确地处理它.

如果有另一种方法来处理多个网页,而不必使用数组和for循环,请告诉我.

arrays bash for-loop

2
推荐指数
1
解决办法
1711
查看次数

标签 统计

for-loop ×2

arrays ×1

bash ×1

newline ×1

perl ×1