Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/perl/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Arrays 在文件中搜索任何匹配项,并为结果指定一个号码,您可以将其作为选项调用_Arrays_Perl_Indexing - Fatal编程技术网

Arrays 在文件中搜索任何匹配项,并为结果指定一个号码,您可以将其作为选项调用

Arrays 在文件中搜索任何匹配项,并为结果指定一个号码,您可以将其作为选项调用,arrays,perl,indexing,Arrays,Perl,Indexing,我想按如下方式运行它 $ perl test.pl tex $perl test.pl tex 结果: 1.得克萨斯州 2.德克萨斯州的休斯敦 3.德克萨斯州DFW 选项2. 电话:德克萨斯州休斯顿 我基本上是想搜索一个数组,并给它指定一个数值,然后每次调用它,而不是完整的值。让文件places.txt包含 然后,下面的Perl脚本执行您想要的操作。脚本中的注释解释 #!/usr/bin/perl use warnings; use strict; use integer; our $fil

我想按如下方式运行它

$ perl test.pl tex $perl test.pl tex 结果: 1.得克萨斯州 2.德克萨斯州的休斯敦 3.德克萨斯州DFW 选项2. 电话:德克萨斯州休斯顿 我基本上是想搜索一个数组,并给它指定一个数值,然后每次调用它,而不是完整的值。

让文件places.txt包含

然后,下面的Perl脚本执行您想要的操作。脚本中的注释解释

#!/usr/bin/perl

use warnings;
use strict;
use integer;

our $filename_place = 'places.txt';
our $index_field_width = 2;

# Read in the place data.
my @place;
open PLACE, '<', $filename_place;
while (<PLACE>) {
    chomp;
    push @place, $_;
}
close PLACE;

# Print a menu.
for (my $i = 0; $i < @place; ++$i) {
    printf "%${index_field_width}d. %s\n", $i+1, $place[$i];
}

# Let the user choose.
print "\nOption? ";
my $option = <>;
chomp $option;
$option >= 1 && $option <= @place
  or die "$0: the option chosen is out of range\n";

# Act on the user's choice.  (Of course, you can put
# here whatever action you like but, as written, the
# following produces your sample output.)
print "Telneting to: ${place[$option-1]}\n\n";
#/usr/bin/perl
使用警告;
严格使用;
使用整数;
我们的$filename_place='places.txt';
我们的$index_字段宽度=2;
#读取位置数据。
我的家;

开放的地方,这是一件比看起来更简单的事情

use strict;
use warnings;

@ARGV == 1 or die "Usage: perl test.pl <location>\n";

my $place = quotemeta shift;

open my $fh, '<', 'telnets.txt' or die $!;
my @telnets = grep /$place/i, <$fh>;
die "No matching telnets\n" unless @telnets;
chomp @telnets;

print "RESULTS:\n";
printf "%d . %s\n", $_ + 1, $telnets[$_] for 0 .. $#telnets;
print "\n";

print "Option? ";

my $option = <STDIN>;
$option =~ s/\s+//g;

die "Invalid selection $option\n" unless $option > 0 and $telnets[$option-1];

print "Telneting to: $telnets[$option-1]\n";
使用严格;
使用警告;
@ARGV==1或die“用法:perl test.pl\n”;
我的$place=quotemeta班次;

打开我的$fh,'遗憾的是,这个问题没有让我明白你想要实现什么。也许输入和输出的例子会有所帮助。(另外,如果您能按惯例大写,我们将不胜感激。)看起来就像将选项加载到哈希数组中一样简单。数组将保持城市名称的顺序,而散列将有一个带有城市的键和一个带有IP地址或主机名的值。如果我理解正确。我同意@coding_hero,如果这就是你所需要的,问题是什么?那么“为我编写程序”服务就不是了。投票结束。@Sinan:如果这是懒惰,作者本可以选择更难免费获得的东西。我相信他不知道如何开始。
#!/usr/bin/perl

use warnings;
use strict;
use integer;

our $filename_place = 'places.txt';
our $index_field_width = 2;

# Read in the place data.
my @place;
open PLACE, '<', $filename_place;
while (<PLACE>) {
    chomp;
    push @place, $_;
}
close PLACE;

# Print a menu.
for (my $i = 0; $i < @place; ++$i) {
    printf "%${index_field_width}d. %s\n", $i+1, $place[$i];
}

# Let the user choose.
print "\nOption? ";
my $option = <>;
chomp $option;
$option >= 1 && $option <= @place
  or die "$0: the option chosen is out of range\n";

# Act on the user's choice.  (Of course, you can put
# here whatever action you like but, as written, the
# following produces your sample output.)
print "Telneting to: ${place[$option-1]}\n\n";
use strict;
use warnings;

@ARGV == 1 or die "Usage: perl test.pl <location>\n";

my $place = quotemeta shift;

open my $fh, '<', 'telnets.txt' or die $!;
my @telnets = grep /$place/i, <$fh>;
die "No matching telnets\n" unless @telnets;
chomp @telnets;

print "RESULTS:\n";
printf "%d . %s\n", $_ + 1, $telnets[$_] for 0 .. $#telnets;
print "\n";

print "Option? ";

my $option = <STDIN>;
$option =~ s/\s+//g;

die "Invalid selection $option\n" unless $option > 0 and $telnets[$option-1];

print "Telneting to: $telnets[$option-1]\n";