输入到程序与​​所有在一个阵列逐个琴弦 [英] input to the program with all the strings in an array one by one

查看:117
本文介绍了输入到程序与​​所有在一个阵列逐个琴弦的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个外壳脚本由如此多的perl脚本,perl脚本之一已经与不同的充输入每次运行和值具有要被存储在在最后一个文件,如图

 #!/ bin / sh的
 .....
 ....
 perl的test.pl苹果
 perl的test.pl芒果
 perl的test.pl香蕉
 ...
...
....

我通过看这些名称生成的文件输入命令行这些名称。

  ** ** names.txt中
苹果
芒果
香蕉

  ** ** names.txt中
    苹果芒果香蕉

他们是用Perl或壳的方式这需要每个名称在一个时间作为输入。结果
即可以names.txt中被视为一个数组,然后Perl脚本使用ARGV或任何其他means.So,我可以有我喜欢

shell脚本需要在一个时间作为输入的每个数组值

 #!/ bin / sh的
     .....
     ....
     perl的test.pl names.txt中
     ....
    ...
    ....


解决方案

编写需要从文件中输入Perl程序的常用方法是构建它们如下:

 而($行=<>){    #做的东西与$线
}

如果文件名在命令行给出的perl会自动打开它们一个接一个,喂线到脚本。如果没有文件名给出,它会从标准输入读取。

但是,如果你写你的脚本这样,你将不能够直接给它的水果在命令行中,他们将不得不在一个文件或标准输入。

要处理多个水果在同一行,你的code可以这样做:

 同时(我的$行=<>){    的Chomp $线;    我的foreach $果(拆',$线){        #使用$水果的东西
    }
}

I have a shell script consisting of so many perl scripts, one of the perl script have to be run with differnt input each time and the value has to be stored in a single file at the end as shown

 #!/bin/sh
 .....
 ....
 perl test.pl apple
 perl test.pl mango
 perl test.pl banana
 ... 
... 
....

I type these names in command lines by looking at the file generated with these names.

**names.txt**
apple
mango
banana

OR

**names.txt**
    apple    mango   banana

Is their a way in perl or shell which takes each name at a time as an input.
That is can names.txt be considered as an array and then the perl script take each array value at a time as an input using ARGV or any other means.So that i can have my shell script like

#!/bin/sh
     .....
     ....
     perl test.pl names.txt
     .... 
    ... 
    ....

解决方案

The usual way to write perl programs that take input from files is to construct them as follows:

while ($line = <>) {

    # do stuff with $line
}

If filenames are given on the command line, perl will automatically open them one by one, feeding the lines to your script. If no filenames are given, it will read from standard input.

But if you write your script this way, you won't be able to give it fruits directly on the command line, they will have to be in a file or standard input.

To handle multiple fruit on the same line, your code can do:

while (my $line = <>) {

    chomp $line;

    foreach my $fruit ( split ' ', $line ) {

        # do something with $fruit
    }
}

这篇关于输入到程序与​​所有在一个阵列逐个琴弦的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆