There is altogether too much wrong with that code to comment on everything. So a brief list of the highlights then some sample code that does what you seem to want:
use strict; use warnings; my $masterStr = <<EOF; john.co.uk,Acton,Backup,Team,backup,bkup-01,External gim.com,Acton,Backup Team,backup,bumas1,internal jeff.be.com,Acton,Backup Team,backup,bumas1,internal scream,Acton,Backup Team,backup,bumas1,internal ACTDFVUA.co.uk,Acton,Backup Team,backup,imrn,internal EOF my $dailyStr = <<EOF; Server,Type,Reason,Data at Risk,Ticket,Resolution john.com,F,No email received,YES gim,Acton,Backup Team,backup,bumas1,internal elephant.com,F No email received,YES jeff.co.com,F No email received,YES scream S,1 email received,Some banana,F,No email received,ES EOF my %masters = loadMasters(); my @dateParts = localtime (); my ($day, $month, $year) = @dateParts[3 .. 5]; $year += 1900; $month += 1; #open my $dailyIn, '<', $INFILE1 or die " cannot open '$INFILE1': $!"; open my $dailyIn, '<', \$dailyStr; <$dailyIn>; # Skip header line while (defined (my $line = <$dailyIn>)) { chomp $line; my ($server, $type, $reason, $status, $ticket, $resolution) = split ',', $line; $_ = uc $_ for $server, $type; $_ //= '-- missing --' for $status, $ticket, $resolution; if ($type eq "F") { $type = "BACKUP FAILED"; } elsif ($type eq "S") { $type = "SKIPPED FILES"; $ticket = "DO NOT RAISE TICKET"; } (my $serverKey = $server) =~ s/\..*//; if (!exists $masters{$serverKey}) { print "'$server' not known please update\n"; next; } printf "%s %s %s,%s,%s,%s,%s,%s,%s,%s,%s,%s, \n", $day, $month, $year, $masters{$serverKey}{loc}, $masters{$serverKey}{team}, $masters{$serverKey}{owner}, $server, $type, $reason, $status, $ticket, $resolution; } sub loadMasters { my %masters; #open my $masterIn, '<', $INFILE1 or die " cannot open '$INFILE1': + $!"; open my $masterIn, '<', \$masterStr; while (defined (my $line = <$masterIn>)) { chomp $line; my ($machine, $loc, $team, $software, $backup, $owner) = split + ',', $line; (my $machineKey = uc $machine) =~ s/\..*//; warn "Multiple servers match '$machineKey'!\n" if exists $masters{$machineKey}; $masters{$machineKey} = { server => $machine, loc => $loc, team => $team, software => $software, backup => $backup, owner => $owner }; } return %masters; }
Prints:
9 5 2014,Acton,Backup,bkup-01,JOHN.COM,BACKUP FAILED,No email received +,YES,-- missing --,-- missing --, 9 5 2014,Acton,Backup Team,internal,GIM,ACTON,Backup Team,backup,bumas +1,internal, 'ELEPHANT.COM' not known please update 9 5 2014,Acton,Backup Team,internal,JEFF.CO.COM,F NO EMAIL RECEIVED +,YES,-- missing --,-- missing --,-- missing --, 'SCREAM S' not known please update 'BANANA' not known please update
The open my $masterIn, '<', \$masterStr; stuff uses a string as a file which is handy for test code like this. The commented out open lines are closer to what you would use for real.
You should really use Text::CSV to manage csv files instead of hand rolled split code.
Update: fixed typo in module name.
In reply to Re: compare csv files only to first fullstop character
by GrandFather
in thread compare csv files only to first fullstop character
by john.tm
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |