http://qs321.pair.com?node_id=1203135

TStanley has asked for the wisdom of the Perl Monks concerning the following question:

Hey there fellow monks! I am trying to help out a client of ours by fixing up an import file of their medical equipment assets that have my company's asset tracking tag on them. Most of the assets in their system were added in without the tags, and they were tracking the MAC addresses of the tags on a separate spreadsheet. I exported the data out of their system (.csv file), and started to match the assets up using the asset identification number for each. One thing I discovered is that some of the assets they had listed on their spreadsheet were not even in the system, so I would have to add them in as well. My concern is the logic where I am comparing the files, setting aside any non-matches, writing to the final output file, and then adding in the items that didn't exist. My code is shown below.


#!C:\Perl64\bin\perl use strict; use warnings; my @input_array; my @output_array; my @extras; my ($INPUT,$data,$OUTPUT); open $INPUT,"<","transfers.csv" || die "Can't open transfer.csv: $!\n" +; open $data,"<","VHC-DVTPump.csv" || die "Can't open VHC-DVTPump.csv: $ +!\n"; open $OUTPUT,">","Output.csv" || die "Can't open Output.csv: $!\n"; while(<$INPUT>){ chomp; my($MAC,$Serial,$ID)=split /,/; #print "MAC: $MAC\tSerial: $Serial\tID: $ID\n"; my %hash=('MAC'=>$MAC,'Serial'=>$Serial,'ID'=>$ID); push @input_array,\%hash; } while(<$data>){ chomp; my($AssetName,$AssetId,$Serial,$Type,$ActivityStatus,$BusinessStatus +,$Description,$PrimaryCategory, $Category1,$Category2,$Category3,$Department1,$Department2,$Depar +tment3,$Department4,$Department5, $Department6,$Group1,$Group2,$NetworkID1,$TagType1,$TagClassifica +tion1,$TagProtected1,$TagDuress1, $NetworkID2,$TagType2,$TagClassification2,$TagProtected2,$TagDure +ss2,$MapID,$GatewayGroup,$X,$Y,$Z, $NISTCertificationDueDate,$RentalReturnDate,$Escalation3,$NISTCer +tificate,$Escalation2,$NISTTag, $ContactName,$Escalation1,$PMCompleted,$RentalInUser,$lastcolumn, +$EOL)=split /,/; my %hash=('Asset Name'=>$AssetName,'Asset Application ID'=>$AssetId, +'Serial Number'=>$Serial,'Asset Type'=>$Type, 'Activity Status'=>$ActivityStatus,'Business Status'=>$Busin +essStatus,'Description'=>$Description, 'Primary Category'=>$PrimaryCategory,'Category 1'=>$Category +1,'Category 2'=>$Category2, 'Category 3'=>$Category3,'Department 1'=>$Department1,'Depar +tment 2'=>$Department2,'Department 3'=>$Department3, 'Department 4'=>$Department4,'Department 5'=>$Department5,'D +epartment 6'=>$Department6, 'Group 1'=>$Group1,'Group 2'=>$Group2,'Network ID 1'=>$Netwo +rkID1,'Tag Type 1'=>$TagType1, 'Tag Classification 1'=>$TagClassification1,'Tag Protected 1 +'=>$TagProtected1,'Tag Duress 1'=>$TagDuress1, 'Network ID 2'=>$NetworkID2,'Tag Type 2'=>$TagType2,'Tag Cla +ssification 2'=>$TagClassification2, 'Tag Protected 2'=>$TagProtected2,'Tag Duress 2'=>$TagDuress +2,'Map ID'=>$MapID,'Gateway Group'=>$GatewayGroup, 'X'=>$X,'Y'=>$Y,'Z'=>$Z,'NIST Certification Due Date'=>$NIST +CertificationDueDate, 'RentalReturnDate'=>$RentalReturnDate,'Escalation3'=>$Escala +tion3,'NIST Certificate'=>$NISTCertificate, 'Escalation2'=>$Escalation2,'NIST Tag'=>$NISTTag,'ContactNam +e'=>$ContactName,'Escalation1'=>$Escalation1, 'PM Completed'=>$PMCompleted,'Rental In User'=>$RentalInUser +,'last column'=>$lastcolumn,'EOL'=>$EOL); push @output_array,\%hash; } close $INPUT; close $data; print $OUTPUT "Asset Name,Asset Application Id,Serial Number,Asset Typ +e,Activity Status,Business Status,Description,Primary Category,Catego +ry 1,Category 2,Category 3,Department 1,Department 2,Department 3,Dep +artment 4,Department 5,Department 6,Group 1,Group 2,Network ID 1,Tag +Type 1,Tag Classification 1,Tag Protected 1,Tag Duress 1,Network ID 2 +,Tag Type 2,Tag Classification 2,Tag Protected 2,Tag Duress 2,Map ID, +Gateway Group,X,Y,Z,NIST Certification Due Date,Rental Return Date,Es +calation3,NIST Certificate,Escalation2,NIST Tag,ContactName,Escalatio +n1,PM Completed,Rental In User,last column,EOL\n"; for my $ref(@input_array){ my $mac=$$ref{'MAC'}; my $ID = $$ref{'ID'}; for my $ref2(@output_array){ my $ID2 = $$ref2{'Asset Application Id'}; my $mac2 = $$ref2{'Network ID 1'}; if($ID = $ID2 and $mac2 = ''){ $$ref2{'Network ID 1'} = $mac; }else{ push @extras,\$ref; } } } for (@output_array){ my $ref = $_; my($AssetName,$AssetId,$Serial,$Type,$ActivityStatus,$BusinessStatus +,$Description, $PrimaryCategory,$Category1,$Category2,$Category3,$Department1,$D +epartment2, $Department3,$Department4,$Department5,$Department6,$Group1,$Grou +p2,$NetworkID1, $TagType1,$TagClassification1,$TagProtected1,$TagDuress1,$Network +ID2,$TagType2, $TagClassification2,$TagProtected2,$TagDuress2,$MapID,$GatewayGro +up,$X,$Y,$Z, $NISTCertificationDueDate,$RentalReturnDate,$Escalation3,$NISTCer +tificate,$Escalation2, $NISTTag,$ContactName,$Escalation1,$PMCompleted,$RentalInUser,$la +stcolumn,$EOL); ## Need to assign the variables their values print $OUTPUT "$AssetName,$AssetId,$Serial,$Type,$ActivityStatus,$Bu +sinessStatus,$Description, $PrimaryCategory,$Category1,$Category2,$Category3, +$Department1,$Department2, $Department3,$Department4,$Department5,$Department6,$Group1,$Grou +p2,$NetworkID1, $TagType1,$TagClassification1,$TagProtected1,$TagDuress1,$Network +ID2,$TagType2, $TagClassification2,$TagProtected2,$TagDuress2,$MapID,$GatewayGro +up,$X,$Y,$Z, $NISTCertificationDueDate,$RentalReturnDate,$Escalation3,$NISTCer +tificate,$Escalation2, $NISTTag,$ContactName,$Escalation1,$PMCompleted,$RentalInUser,$la +stcolumn,$EOL\n"; } for (@extras){ my $ref = $_; my($AssetName,$AssetId,$Serial,$Type,$ActivityStatus,$BusinessStatus +,$Description, $PrimaryCategory,$Category1,$Category2,$Category3,$Department1,$Depa +rtment2, $Department3,$Department4,$Department5,$Department6,$Group1,$Group2, +$NetworkID1, $TagType1,$TagClassification1,$TagProtected1,$TagDuress1,$NetworkID2 +,$TagType2, $TagClassification2,$TagProtected2,$TagDuress2,$MapID,$GatewayGroup, +$X,$Y,$Z, $NISTCertificationDueDate,$RentalReturnDate,$Escalation3,$NISTCertif +icate,$Escalation2, $NISTTag,$ContactName,$Escalation1,$PMCompleted,$RentalInUser,$lastc +olumn,$EOL); ## Need to assign the variables their values print $OUTPUT "$AssetName,$AssetId,$Serial,$Type,$ActivityStatus,$B +usinessStatus,$Description, $PrimaryCategory,$Category1,$Category2,$Category3, +$Department1,$Department2, $Department3,$Department4,$Department5,$Department6,$Group1,$Grou +p2,$NetworkID1, $TagType1,$TagClassification1,$TagProtected1,$TagDuress1,$Network +ID2,$TagType2, $TagClassification2,$TagProtected2,$TagDuress2,$MapID,$GatewayGro +up,$X,$Y,$Z, $NISTCertificationDueDate,$RentalReturnDate,$Escalation3,$NISTCer +tificate,$Escalation2, $NISTTag,$ContactName,$Escalation1,$PMCompleted,$RentalInUser,$la +stcolumn,$EOL\n"; } close $OUTPUT;

TStanley
--------
People sleep peaceably in their beds at night only because rough men stand ready to do violence on their behalf. -- George Orwell