我无法理解这应该如何工作。我在while循环之外定义了两个哈希。目标是能够检测shell类型,并将适当的散列值分配给新的$ shell变量。这是我目前的尝试代码..Perl关联数组和变量赋值
#!/usr/bin/perl
use strict;
use warnings;
use POSIX;
use DateTime;
use Term::ANSIColor qw(:constants);
local $Term::ANSIColor::AUTORESET = 1;
my $dt = DateTime->now; # Stores current date and time as datetime object
my %multicity = (
url => "example.com",
ftpuser => "user",
ftppass => "pass",
remote_dir => "/httpdocs/",
dbname => "database"
);
my %singlecity = (
url => "example2.com",
ftpuser => "user",
ftppass => "pass",
remote_dir => "/httpdocs/",
dbname => "database"
);
open (MYFILE, 'sites.txt');
LINE: while (<MYFILE>) {
next LINE if /^#/;
my ($shelltype, $siteurl, $ftpuser, $ftppass, $dbname) = split /\|/;
# 1|example.com|user|pass|databasename - This should be a singlecity shell type.
if ($shelltype == 1) { my $shell = \%multicity; } else { my $shell = \%singlecity; };
print "Shelltype: $shelltype\n";
print "Should be $shell{url}\n";
}
close (MYFILE);
我已经尝试了很多不同的东西有没有用,所以我终于诉诸这里的利弊在计算器得到一些方向!
任何帮助,非常感谢。谢谢。
这并获得成功。谢谢@simbabque。非常有意义。其他人的答案几乎相同,但看起来像你先回答。 – Brandon