perl 将字符串分割成二维数组

发布于 2024-09-08 17:18:17 字数 764 浏览 1 评论 0原文

好吧..这有效......

sub getApSrvs
{
my %apsrv;
my $cluster;

  foreach $cluster (getClusters())
  {
  $apsrv{$cluster} = [split('\s+', `/$cluster/bin/gethosts -t app|sort -u`)];
  }
return %apsrv;
}

现在我如何在火腿三明治中让它像这样打印 $cluster --> $hostname

好吧,我添加了:

my %apsrv = getApSrvs();
for my $cluster (keys %apsrv) {
print "$cluster -> $apsrv{$cluster}\n";
}

然后我得到...

qboc22->数组(0x9111618)

qboc5->数组(0x9111504)

qboc32->数组(0x90e20cc)

qboc28->数组(0x90e1d28)

qboc30->数组(0x90e1f38)

qboc23->数组(0x9111540)

qboc27->数组(0x911181c)

qboc29->数组(0x91115ac)

qbo->数组(0x90e2294)

Okay .. this works ...

sub getApSrvs
{
my %apsrv;
my $cluster;

  foreach $cluster (getClusters())
  {
  $apsrv{$cluster} = [split('\s+', `/$cluster/bin/gethosts -t app|sort -u`)];
  }
return %apsrv;
}

... now how in the ham sandwich do I get this to print like so $cluster --> $hostname

okay I added :

my %apsrv = getApSrvs();
for my $cluster (keys %apsrv) {
print "$cluster -> $apsrv{$cluster}\n";
}

and I get ...

qboc22 -> ARRAY(0x9111618)

qboc5 -> ARRAY(0x9111504)

qboc32 -> ARRAY(0x90e20cc)

qboc28 -> ARRAY(0x90e1d28)

qboc30 -> ARRAY(0x90e1f38)

qboc23 -> ARRAY(0x9111540)

qboc27 -> ARRAY(0x911181c)

qboc29 -> ARRAY(0x91115ac)

qbo -> ARRAY(0x90e2294)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

征﹌骨岁月お 2024-09-15 17:18:17

$apsrv{$cluster} 是对数组的引用,因此如果你想打印它的内容,你可以这样做:

my %apsrv = getApSrvs();
for my $cluster (keys %apsrv) {
    print "$cluster -> ", join(', ', @$apsrv{$cluster}), "\n";
}

$apsrv{$cluster} is a reference to an array, so if you want to print the contents of it you can do :

my %apsrv = getApSrvs();
for my $cluster (keys %apsrv) {
    print "$cluster -> ", join(', ', @$apsrv{$cluster}), "\n";
}
微暖i 2024-09-15 17:18:17
my %apsrv = getApSrvs();
for my $cluster (keys %apsrv) {
    print "$cluster -> $apsrv{$cluster}\n";
}

如果顺序很重要,您将需要在打印之前对键进行排序(排序键 %apsrv)。

my %apsrv = getApSrvs();
for my $cluster (keys %apsrv) {
    print "$cluster -> $apsrv{$cluster}\n";
}

You will want to sort the keys (sort keys %apsrv) before printing if the order is important.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文