Opening multiple files in perl array -
i have perl script assigned files .log
extension array called @allfiles
. how run script files stored in each array? idea open(my $fn, '<', @allfiles) or die "could not open file '@files': $!";
incorrect read files in array @ once. help?
#!/usr/bin/perl use strict; use warnings; use posix 'strftime'; #my $filename = 'igxleventlog.3.17.2015.20.25.12.625.log'; $directory = "/opt/lampp/htdocs/otpms/data"; @allfiles = glob("*.log"); opendir (my $dir, $directory) or die "could not open directory '$directory': $!"; open(my $fn, '<', $filename) or die "could not open file '$filename': $!"; our @output; %details; capturedata(); close ($fn); $timestamp = strftime '%y-%m-%d.%h:%m:%s', localtime; @output = $timestamp .'.sql'; open(my $fh, '>', @output) or die "could not create file '@output': $!"; createfile(); close($fh); sub capturedata { while(my $row = <$fn>) { chomp $row; if ($row =~ /computer name:\s*(\s+)/i ) # match computer name white space non white space { $details{tester_name} = $1; } elsif ($row =~ /operating system:\s*(.*\s)/i ) # match operating system white space word { $details{op_sys} = $1; } elsif ($row =~ /ig-xl version:\s*([^;]*)/i ) # match ig-xl version white space semi colon { $details{igxl_vn} = $1; } elsif ($row =~ /^([\d.]+)\s+(\s+)(?=\s)/ ) #match slot white space , non white space { push @{$details{slot}}, $1; push @{$details{board_name}}, $2; } } } # capturedata sub createfile { $log_time_stamp = (stat($filename))[9]; ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst)=localtime($log_time_stamp); $nice_timestamp = sprintf ( "%04d-%02d-%02d %02d:%02d:%02d", $year+1900,$mon+1,$mday,$hour,$min,$sec); print $nice_timestamp; (my $i = 0; $i < @{$details{slot}}; $i++) { print {$fh} "insert testerdevicematrix.tbl_tester_info" ."(tester_name, operating_system, version, board_name, config , date_modified, log_created) " ."values ('$details{tester_name}', '$details{op_sys}', '$details{board_name}[$i]', " ."'$details{igxl_vn}', '$details{slot}[$i]', '$timestamp', '$nice_timestamp');\n"; } } # createfile
you need enclose code handles single file in loop iterates on of log files
you should reconsider amount of comments use. far better write code , choose identifiers behaviour self-explanatory. counter-productive wrap parts of process in subroutines
this how implement problem. haven't been able test except extent compiles
#!/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 /; $now_timestamp = localtime->strftime('%y-%m-%d.%h:%m:%s'); open $out_fh, '>', "$now_timestamp.sql"; chdir log_dir; while ( $logfile = glob '*.log' ) { warn "processing $logfile\n"; open $log_fh, '<', $logfile; %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; } } $stat = stat $logfile; $log_timestamp = localtime($stat->mtime)->strftime('%y-%m-%d %h:%m:%s'); $i ( 0 .. $#{ $details{slot} } ) { @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 testerdevicematrix.tbl_tester_info (%s) values (%s);\n", join(', ', columns), join(', ', map "'$_'", @values); } } close $out_fh;
Comments
Post a Comment