1

我对 perl 很陌生,并且正在努力让这个脚本工作。

我已经采取了碎片或 perl 并将它们作为单独的部分工作,但是在尝试将它们混合在一起时它失败了。即使出现错误消息,我也找不到我的错误在哪里。

工作和完成时的脚本将读取一个输出文件并通过它的部分我的部分,并最终生成一个新的输出文件,其中包含一些附加文本的标题和该部分中的行数值。

我的问题是,当它为数组中的每个关键字执行循环时,它现在失败并出现错误消息“参数”不是数组元素中的数字“。Perl 将我引导到脚本中的一个部分,但我看不到我是如何错误地调用该元素的。数组中的所有元素都是 alpha,但错误消息是指数值。

谁能看到我的错误。

谢谢

这是脚本

#!/usr/bin/perl -w
use strict;
use warnings;
use diagnostics;
# this version reads each variable and loops through the 18 times put only displays on per loop. 

my $NODE = `uname -n`;
my $a = "/tmp/";
my $b = $NODE ;
my $c = "_deco.txt";
my $d = "_deco_mini.txt";
chomp $b;
my $STRING = "$a$b$c";
my $STRING_out = "$a$b$d";

my @keyword = ( "Report", "Last", "HP", "sulog", "sudo", "eTrust", "proftp", "process", "active clusters", "pdos", "syslog", "BNY", "syslogmon", "errpt", "ports", "crontab", "NFS", "scripts", "messages");

my $i = 0;
my $keyword="";
my $x=0;
my $y=0;
my $jw="";

my $EOS = "########################################################################";
my $qty_lines=0;
my $skip5=0;
my $skipcnt=0;
my $keeplines=0;
my @HPLOG="";

do {
    print "Reading File: [$STRING]\n";

    if (-e "$STRING" && open (IN, "$STRING")) {
#       ++$x;                              # proving my loop worked
#       print "$x interal loop counter\n"; # proving my loop worked
        for ( ++$i) { # working
            while ( <IN> ) {
                chomp ;
                #if ($_ =~ /$keyword/) {
                #if ($_ =~ / $i /) {
                #if ($_ =~ /$keyword[ $i ]/) {
                if ($_ =~ /$keyword $i/) {
                    print "  $i \n";
                    $skip5=1;
                    next;
#                   print "$_\n";# $ not initalized error when tring to use it 
                }

                if ($skip5) {
                    $skipcnt++;
                    print "SKIP LINE: $_\n";
                    print "Header LINE: $_\n";
                    next if $skipcnt <= 5;
                    $skip5=0;
                    $keeplines=1;
                }

                if ($keeplines) {
#                   ++$qty_lines;                # for final output
                    last if $_ =~ /$EOS/;
                    print "KEEP LINE: $_\n";
#                   print "$qty_lines\n";      # for final output

                    push @HPLOG, "$_\n";
                    # push @HPLOG, "$qty_lines\n";# for final output
                }
            } ## end while ( <IN> )
        } ## end for ( ++$i)
    } ## end if (-e "$STRING" && open (IN, "$STRING"))

    close (IN);
} while ( $i < 19 &&  ++$y < 18 );

这是一个示例部分或输入文件。################################################# #############################

                       Checking for active clusters.

                                @@@@@@@@@

  root 11730980 12189848   0 11:24:20  pts/2  0:00 egrep hagsd|harnad|HACMP|haemd

 If there are any processes listed you need to remove the server from the cluster.

############################################################################

                       This is the output from Pdos log

Please review it for anything that looks like a users may be trying to run something.

                                @@@@@@@@@

                        This server is not on Tamos 

############################################################################

                        This is the output from syslog.conf.

Look for any entries on the right side column that are not the ususal logs or location.

                                 @@@@@@@@@

# @(#)34    1.11  src/bos/etc/syslog/syslog.conf, cmdnet, bos610 4/27/04 14:47:53
# IBM_PROLOG_BEGIN_TAG 
# This is an automatically generated prolog. 
#  
# bos610 src/bos/etc/syslog/syslog.conf 1.11 

我截断了文件的其余部分

4

1 回答 1

10

谁能看到我的错误。

我可以看到很多错误。但我也看到了一些好东西,比如use strictand use warnings

我对您的建议是改进您的编码风格,以便您和其他人更容易调试任何问题。

命名变量

my $NODE = `uname -n`;
my $a = "/tmp/";
my $b = $NODE ;
my $c = "_deco.txt";
my $d = "_deco_mini.txt";
chomp $b;
my $STRING = "$a$b$c";
my $STRING_out = "$a$b$d";

为什么其中一些名称全部大写而其他名称全部小写?如果要建立文件名,为什么要调用保存文件名的变量$STRING

my @keyword = ( "Report", "Last", "HP", "sulog", "sudo", ....

如果您有一个包含多个关键字的列表,那么不选择单数作为变量名不是很容易吗?怎么样@keywords

使用不需要的临时变量

my $NODE = `uname -n`;
my $a = "/tmp/";
my $b = $NODE ;
my $c = "_deco.txt";
chomp $b;
my $STRING = "$a$b$c";

为什么需要$a$b$c这些变量的(原谅我)愚蠢的名字是你不需要它们的迹象。这个怎么样?

my $node_name = `uname -n`;
chomp $node_name;
my $file_name = sprintf '/tmp/%s/_deco.txt', $node_name;

你最大的问题:你不知道如何使用数组

当涉及到数组时,您犯了几个严重的错误。

my @HPLOG="";

你想要一个数组还是另一个字符串?@说数组,说""字符串。我猜你想要一个新的空数组,所以my @hplog = ()会好得多。但是由于没有必要告诉 perl 你想要一个空数组,因为无论如何它都会给你一个空数组,所以可以my @hplog;很好地完成这项工作。

我花了一段时间才弄清楚下一个,我仍然不确定我是否正确猜测你的意图:

my @keyword = ( "Report", "Last", "HP", "sulog", "sudo", "eTrust", "proftp", "process", "active clusters", "pdos", "syslog", "BNY", "syslogmon", "errpt", "ports", "crontab", "NFS", "scripts", "messages");
...
                if ($_ =~ /$keyword $i/) {

认为您在这里所做的是尝试将您当前的输入行$i@keywords. 如果我的假设是正确的,那么您真的很想说:

if ( /$keyword[ $i ]/ ) {

迭代数组

Perl 不是 C。它不会让你跳过箍来获得循环。

只需查看您为循环关键字而编写的所有代码:

my $i = 0;
...
        for ( ++$i) { # working
...
                if ($_ =~ /$keyword $i/) {
...
} while ( $i < 19 &&  ++$y < 18 );

除了您的working评论只是自欺欺人以及您硬编码数组中元素的数量这一事实之外,您还可以使用for-each循环:

foreach my $keyword ( @keywords ) {
    # more code here
}

我敢肯定,当您尝试处理上述列表时,您在这里提出的问题将会消失。玩得开心。

于 2013-06-22T08:32:30.817 回答