【问题标题】:Perl: Counting script doing wrong?Perl:计数脚本做错了吗?
【发布时间】:2014-02-11 13:21:20
【问题描述】:

我的脚本:

#!/usr//bin/perl
#
# Script to balance accounts between servers
# By Philip Gabrielsen
#    
use strict;
use warnings;    
START:
print "\nZimbra account moving script\n";
print "First we will collect data from Zimbra, this may take a while.\n\n";

my %accounts;
DATACOLLECT:
print "Collecting Zimbra mailbox server(s)... ";
my $servers = `zmprov gas mailbox`;
print "OK\n";

print "Collecting numbers of accounts per server... ";
foreach my $server (split(/\n/, $servers)) {
  $accounts{$server} = `zmprov -l gaa -s $server|wc -l`;
}
print "OK\n";

foreach my $server (keys %accounts) {
  print "\nServer $server with ". $accounts{$server} ." accounts\n";
}

print "TEST, is total number of accounts good?";
$accounts{'total'} = 0;
foreach my $server1 (keys %accounts) {
  $accounts{'total'} = $accounts{'total'} + $accounts{$server1};
  print "\nAdded $accounts{$server1} and total is now $accounts{'total'}";
}
print "\nTotal number of accounts: $accounts{'total'}\n";

输出: [zimbra@snake tmp]$ perl accounts.pl

Zimbra account moving script
First we will collect data from Zimbra, this may take a while.

Collecting Zimbra mailbox server(s)... OK
Collecting numbers of accounts per server... OK

Server snake with 363
 accounts

Server tiger with 431
 accounts

Server lion with 273
 accounts
TEST, is total number of accounts good?
Added 363
 and total is now 363
Added 431
 and total is now 794
Added [zimbra@tiberius tmp]$ perl accounts.pl

Zimbra account moving script
First we will collect data from Zimbra, this may take a while.

Collecting Zimbra mailbox server(s)... OK
Collecting numbers of accounts per server... OK

Server titus.zimbra.h.bitbit.net with 363
 accounts

Server tiberius.zimbra.h.bitbit.net with 431
 accounts

Server otho.zimbra.h.bitbit.net with 273
 accounts
TEST, is total number of accounts good?
Added 363
 and total is now 363
Added 431
 and total is now 794
Added 1588 and total is now 1588
Added 273
 and total is now 1861
Total number of accounts: 1861 and total is now 1588
Added 273
 and total is now 1861
Total number of accounts: 1861

正如第一次看到的,当列出每个服务器的帐户时,会显示正确的数字,但最后一部分,当我想将所有 $accounts 添加到总值中时,会弹出数字 1588,这应该是 273.. .

【问题讨论】:

  • Added [zimbra@tiberius tmp]$ perl accounts.pl 中间的那一行是什么,看起来你又运行了脚本?

标签: perl zimbra


【解决方案1】:

我不得不承认,有一段时间你让我难过。但后来我意识到这段代码不正确:

$accounts{'total'} = 0;                 # here you add a key to the serverlist
foreach my $server1 (keys %accounts) {  # here you list all keys
  $accounts{'total'} = $accounts{'total'} + $accounts{$server1};
  print "\nAdded $accounts{$server1} and total is now $accounts{'total'}";
}

因为键total是同一个哈希中的键之一:

titus.zimbra.h.bitbit.net
tiberius.zimbra.h.bitbit.net
otho.zimbra.h.bitbit.net 
total

因此,当您到达列表中键 total 的点时,您会看到总数翻了一番,从 794 增加到 1588。

答案是不使用相同的哈希值来存储您的总和。事实上,为什么要使用哈希?

my $total = 0;                    # independent scalar 
foreach my $server1 (keys %accounts) {
  $total += $accounts{$server1};
  print "\nAdded $accounts{$server1} and total is now $total";
}

另外值得注意的是你在cmets中说chomp把你所有的数字都变成了1。你可能用错了chomp,这是初学者常见的错误。

$count = chomp($count);      # WRONG! chomp returns number of newlines removed
chomp($count);               # Correct. chomp modifies its argument variable

你可能会问chomp返回删除的换行数有什么好处,答案是它也可以用于数组和哈希:

my $removed = chomp(@array); # See how many newlines were removed 

【讨论】:

    【解决方案2】:

    在读取 Zimbra 命令后,您需要 chomp() 输入行 - 这就是为什么在“...and total is now”之前有一个换行符。

    【讨论】:

    • 我知道,但是 chmop() 将我所有的值都设为 1,但这不是这里的问题。这是计数部分...
    • 你做过$x = chomp($x);之类的事情吗? chomp 修改参数并返回删除的字符数,您应该改为 chomp($x)
    猜你喜欢
    • 2021-04-13
    • 1970-01-01
    • 2014-07-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-04-28
    • 2012-01-15
    相关资源
    最近更新 更多