Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/12.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
Arrays 在perl数组中打开多个文件_Arrays_Perl - Fatal编程技术网

Arrays 在perl数组中打开多个文件

Arrays 在perl数组中打开多个文件,arrays,perl,Arrays,Perl,我有一个perl脚本,在该脚本中,我将扩展名为.log的所有文件分配给一个名为@allfiles的数组。如何为存储在每个阵列中的文件运行脚本?我的想法类似于open(My$fn),您只需要将处理单个文件的代码封装在循环中,循环遍历所有日志文件 您还应该重新考虑您使用的注释量。最好编写代码并选择标识符,以便行为能够自我解释。将流程的一部分封装在子例程中也会适得其反 这就是我将如何实现您的问题。我无法进行测试,除非它能够编译 #!/usr/bin/perl use strict; use warn

我有一个perl脚本,在该脚本中,我将扩展名为
.log
的所有文件分配给一个名为
@allfiles
的数组。如何为存储在每个阵列中的文件运行脚本?我的想法类似于
open(My$fn),您只需要将处理单个文件的代码封装在循环中,循环遍历所有日志文件

您还应该重新考虑您使用的注释量。最好编写代码并选择标识符,以便行为能够自我解释。将流程的一部分封装在子例程中也会适得其反

这就是我将如何实现您的问题。我无法进行测试,除非它能够编译

#!/usr/bin/perl

use strict;
use warnings;
use 5.010;
use autodie;

use File::stat;
use Time::Piece;

use constant LOG_DIR => '/opt/lampp/htdocs/otpms/Data';
use constant COLUMNS => qw/ tester_name operating_system version board_name config date_modified log_created /;

my $now_timestamp = localtime->strftime('%Y-%m-%d.%H:%M:%S');
open my $out_fh, '>', "$now_timestamp.sql";

chdir LOG_DIR;

while ( my $logfile = glob '*.log' ) {

    warn "Processing $logfile\n";

    open my $log_fh, '<', $logfile;

    my %details;

    while ( <$log_fh> ) {

        if ( /Computer Name:\s*(\S+)/i ) {
            $details{tester_name} = $1;
        }
        elsif ( /Operating System:\s*(.*\S)/i ) {
            $details{op_sys} = $1;
        }
        elsif ( /IG-XL Version:\s*([^;]*)/i ) {
            $details{igxl_vn} = $1;
        }
        elsif ( /^([\d.]+)\s+(\S+)/ ) {
            push @{ $details{slot} }, $1;
            push @{ $details{board_name} }, $2;
        }
    }

    my $stat          = stat $logfile;
    my $log_timestamp = localtime($stat->mtime)->strftime('%Y-%m-%d %H:%M:%S');

    for my $i ( 0 .. $#{ $details{slot} } ) {

        my @values = (
            $details{tester_name},
            $details{op_sys},
            $details{board_name}[$i],
            $details{igxl_vn},
            $details{slot}[$i],
            $now_timestamp,
            $log_timestamp,
        );

        printf {$out_fh} "INSERT INTO TesterDeviceMatrix.TBL_TESTER_INFO (%s) VALUES (%s);\n",
            join(', ', COLUMNS),
            join(', ', map "'$_'", @values);
    }
}

close $out_fh;
!/usr/bin/perl
严格使用;
使用警告;
使用5.010;
使用自动模具;
使用File::stat;
使用时间::件;
使用常量LOG_DIR=>'/opt/lampp/htdocs/otpms/Data';
使用常量列=>qw/tester\u name operating\u system version board\u name config date\u modified log\u created/;
我的$now\u timestamp=localtime->strftime(“%Y-%m-%d.%H:%m:%S”);
打开我的$out\u fh,“>”,“$now\u timestamp.sql”;
chdir LOG_DIR;
而(my$logfile=glob'*.log'){
警告“正在处理$logfile\n”;

打开我的$log\u fh,'你好,Borodin。是的,我理解这个想法,只是一直在想办法把它投入使用。不过,我有一个小问题,
Time::Piece
是一个模块吗?我不太确定,但我想我使用的服务器上没有它。它返回一条错误消息,说
找不到Time/Piece.pm
@hzq:是的从perl的v5.10开始就是一个核心模块——大约八年前。听起来它好像不在您的服务器上,所以它确实应该更新了。嗯,我刚刚检查了版本,它是5.10.1,所以我不确定为什么会收到错误消息。有没有其他方法可以解决这个问题?我正在尝试用
POSIX'strftime'
来代替它。@hzq:它应该定义我我会在5.10.1上出现。你应该和系统管理人员谈谈。别认为这是可能的哈哈。我会用我问题中的方法。我在
my$now\u timestamp=localtime->strftime(“%Y-%m-%d.%H:%m:%s”)上有一个错误;
无法通过包“Tue Jun 16 08:42:21 2015”找到对象方法“strftime”(也许你忘记加载了)“2015年6月16日星期二08:42:21”?)在exTester.pl第13行。
如果您愿意,我可以编辑这个问题,通过整合您的答案来展示我迄今为止所做的工作。
#!/usr/bin/perl

use strict;
use warnings;
use 5.010;
use autodie;

use File::stat;
use Time::Piece;

use constant LOG_DIR => '/opt/lampp/htdocs/otpms/Data';
use constant COLUMNS => qw/ tester_name operating_system version board_name config date_modified log_created /;

my $now_timestamp = localtime->strftime('%Y-%m-%d.%H:%M:%S');
open my $out_fh, '>', "$now_timestamp.sql";

chdir LOG_DIR;

while ( my $logfile = glob '*.log' ) {

    warn "Processing $logfile\n";

    open my $log_fh, '<', $logfile;

    my %details;

    while ( <$log_fh> ) {

        if ( /Computer Name:\s*(\S+)/i ) {
            $details{tester_name} = $1;
        }
        elsif ( /Operating System:\s*(.*\S)/i ) {
            $details{op_sys} = $1;
        }
        elsif ( /IG-XL Version:\s*([^;]*)/i ) {
            $details{igxl_vn} = $1;
        }
        elsif ( /^([\d.]+)\s+(\S+)/ ) {
            push @{ $details{slot} }, $1;
            push @{ $details{board_name} }, $2;
        }
    }

    my $stat          = stat $logfile;
    my $log_timestamp = localtime($stat->mtime)->strftime('%Y-%m-%d %H:%M:%S');

    for my $i ( 0 .. $#{ $details{slot} } ) {

        my @values = (
            $details{tester_name},
            $details{op_sys},
            $details{board_name}[$i],
            $details{igxl_vn},
            $details{slot}[$i],
            $now_timestamp,
            $log_timestamp,
        );

        printf {$out_fh} "INSERT INTO TesterDeviceMatrix.TBL_TESTER_INFO (%s) VALUES (%s);\n",
            join(', ', COLUMNS),
            join(', ', map "'$_'", @values);
    }
}

close $out_fh;