Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/perl/10.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
在Perl数组中移动值_Perl - Fatal编程技术网

在Perl数组中移动值

在Perl数组中移动值,perl,Perl,我很难以一致的格式放置数组列。我有以下输出: Mon,Jun,25,14:39:29,2012,971,29,0,25,0,0,0,4,Mon,Jun,25,14:39:29,2012,25,mod_was_ap22_http.c Mon,Jun,25,14:40:29,2012,972,28,0,25,0,0,0,3,Mon,Jun,25,14:40:29,2012,3,mod_sm22.cpp,22,mod_was_ap22_http.c Mon,Jun,25,14:41:2

我很难以一致的格式放置数组列。我有以下输出:

Mon,Jun,25,14:39:29,2012,971,29,0,25,0,0,0,4,Mon,Jun,25,14:39:29,2012,25,mod_was_ap22_http.c
    Mon,Jun,25,14:40:29,2012,972,28,0,25,0,0,0,3,Mon,Jun,25,14:40:29,2012,3,mod_sm22.cpp,22,mod_was_ap22_http.c
    Mon,Jun,25,14:41:29,2012,973,27,0,24,0,0,0,3,Mon,Jun,25,14:41:29,2012,24,mod_was_ap22_http.c
    Mon,Jun,25,14:42:29,2012,974,26,0,20,0,0,0,6,Mon,Jun,25,14:42:29,2012,1,mod_sm22.cpp,19,mod_was_ap22_http.c
    Mon,Jun,25,14:43:29,2012,971,29,0,26,0,0,0,3,Mon,Jun,25,14:43:29,2012,2,mod_sm22.cpp,24,mod_was_ap22_http.c
    Mon,Jun,25,14:44:30,2012,957,43,0,41,0,0,0,2,Mon,Jun,25,14:44:30,2012,1,mod_sm22.cpp,40,mod_was_ap22_http.c
    Mon,Jun,25,14:45:30,2012,963,37,0,35,0,0,0,2,Mon,Jun,25,14:45:30,2012,2,mod_sm22.cpp,32,mod_was_ap22_http.c
    Mon,Jun,25,14:46:30,2012,972,28,0,24,1,1,0,2,Mon,Jun,25,14:46:30,2012,24,mod_was_ap22_http.c,1,ApacheModule.cpp
    Mon,Jun,25,14:47:30,2012,961,39,1,37,0,0,0,1,Mon,Jun,25,14:47:30,2012,37,mod_was_ap22_http.c,1,ApacheModule.cpp
    Mon,Jun,25,14:48:30,2012,968,32,0,30,0,0,0,2,Mon,Jun,25,14:48:30,2012,30,mod_was_ap22_http.c
    Mon,Jun,25,14:49:30,2012,972,28,0,25,0,0,0,3,Mon,Jun,25,14:49:30,2012,1,mod_sm22.cpp,24,mod_was_ap22_http.c
我希望列显示: 周、月、日、时间、年、Rdy、Bsy、Rd、Wr、Ka、日志、Dns、Cls、AP22、SM22、APACHEMODEL

当前,粗体显示的列不按顺序排列(其余的都是正确的)。每一行的格式都不一致。该生产线有时先有ap22,有时先有sm22,有时没有或全部有三个模块。模块前的编号与模块相关。如何将数据移动到一致的格式

请注意,每行中的第2个日期mod_was_http.c、mod_sm22.cpp和ApacheModule.cpp将在最终数组中删除

以下是我目前的代码:

# This program parses a error log for necessary information and outputs in CSV format.

# chunks of your input to ignore, see below... 
my %ignorables = map { $_ => 1 } qw([notice mpmstats: rdy bsy rd wr ka log dns cls bsy: in);  

# 3-arg open is safer than 2, lexical my $fh better than a global FH glob 
open my $error_fh, '<', 'iset_error_log';   

sub findLines {
    my($item,@result)=("");
    # Iterates over the lines in the file, putting each into $_ 
    while (<$error_fh>) {      

        # Select only those fields that have the word 'notice'
        if (/\[notice/) {          

            # Place those lines with the word 'rdy' on the next line
            if (/\brdy\b/){
                push @result,"$item\n";
                $item="";

            }
            else {
                $item.=",";
            }

            # Split the line into fields, separated by spaces, skip the %ignorables         
            my @line = grep { not defined $ignorables{$_} } split /\s+/;    

            # More cleanup         
            s/|^\[|notice|[]]//g for @line; # remove unnecessary elements from the array

            # Output the line.  
            @line = join(",", @line);          
            s/,,/,/g for @line;
            map $item.=$_, @line;
            }
        } 
        @result
    }  

my @array = &findLines;
foreach $line (@array){
    print $line; #This is where I would like to organize the lines if possible.
}

在使用“连接”将列转换回一行文本之前,您可能希望在仍有拆分的情况下对列重新排序

你只需要交换一下

# 0        ,1    ,2  ,3   ,4   ,5  ,6  ,7 ,8 ,9 ,10 ,11 ,12 ,13  ,14  ,15
# DayOfWeek,Month,Day,Time,Year,Rdy,Bsy,Rd,Wr,Ka,Log,Dns,Cls,AP22,SM22,ApacheModule
#
# Sometimes the last 2 fields are missing and 13 comes before 14 and 15 in the 
# input, so fix that.
if (@line < 16) {
    push @line, '', ''; # or whatever you want for blanks
}

@line = @line[0..12,14,15,13]; # rearrange the array
#0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15
#周、月、日、时间、年、Rdy、Bsy、Rd、Wr、Ka、Log、Dns、Cls、AP22、SM22、ApacheModule
#
#有时,最后2个字段丢失,在字段中,13出现在14和15之前
#输入,所以修复它。
如果(@line<16){
按@line、、''.#或任何你想要的空格键
}
@line=@line[0..12,14,15,13];#重新排列数组
此外,如果使用空字符串(
“”
)作为空字段,正则表达式
s/、/、/g
将打破此限制。缺少最后一个字段的短行将返回为缺少正确的13和14字段的短行


根据这里和之前提出的各种问题,我强烈建议您获得一份(可供下载或购买)或更好地掌握整个语言。我最近读了很多前一个版本,并很喜欢它,并且从后一个版本的早期版本中获得了我最初的大部分Perl知识。

输入与示例输出不匹配,日期不同。如果您想让我们解决一个特定的输入问题,您应该提供适当的输入来测试它。@TLP:很抱歉,它已经用正确的输出进行了更新。输出仍然与输入不匹配。时间戳不一样,行太少。请删除不相关的错误行,并包含更多的通知行。
# 0        ,1    ,2  ,3   ,4   ,5  ,6  ,7 ,8 ,9 ,10 ,11 ,12 ,13  ,14  ,15
# DayOfWeek,Month,Day,Time,Year,Rdy,Bsy,Rd,Wr,Ka,Log,Dns,Cls,AP22,SM22,ApacheModule
#
# Sometimes the last 2 fields are missing and 13 comes before 14 and 15 in the 
# input, so fix that.
if (@line < 16) {
    push @line, '', ''; # or whatever you want for blanks
}

@line = @line[0..12,14,15,13]; # rearrange the array