MyJeweledPerls has asked for the wisdom of the Perl Monks concerning the following question:
Hola Perl Monks, I seek your wisdom. I have an array of ID's (@sample_ids in the code below) that have been flagged as being faulty for some reason. I have the CSV file that contains all the original data and want to print out rows with the sample IDs in my array. So far I have written the following code
#!/usr/bin/perl use strict; use warnings; my @error_array; my @sample_ids; my @merged_array; #Opening File Number 1 open (ERRORFILE,"<", "Errors-email.txt") || die "File not found\n"; @error_array = <ERRORFILE>; close ERRORFILE; #Opening File Number 2 open (CSV,"<", "Merged_CSVfiles.csv") || die "File not found\n"; @merged_array = <CSV>; close CSV; #Getting the sample ids foreach my $line(@error_array){ if($line =~ m/^Sample_identifier/){ $line =~ s/Sample_identifier//; $line =~ s/IRD.+:$//; $line =~ s/\///; $line =~ s/^\s//; $line =~ s/IRD.+$//; push(@sample_ids,$line); # print(@sample_ids); }# End of the if loop }#End of the for-each loop foreach my $records(@merged_array){ if($records =~ /$sample_ids[3]/){ print $records; } }
I have noted that when I put in UCD67832 (the sample id stored as the 4th element in the array) into the matching statement instead of the sample_ids array element that's there, it is able to return the line no problem. I'm not sure what is the problem. I had tried nested for-each loops before but I bungled that. I have been trying to understand why this doesn't work but so far nothing. -Thanks for your time in advance or retroactively Dave-O
Update sorry about that let me share my data. My CSV has 28 fields in it like for instance and each line represents a record. My @sample_ids has in this specific case 438 ids.
What I wanted to be able to do is to have an expression like this foreach $line ( I called it record in the original code) if there is a match to an element in my arrays of sample_ids then to print out that line. In the code I put $sample_ids3 as a test to see if the line/record would print but it didnt.Name,Building,Processing Date,Receipt Date, Location,Patient Id,Sample + ID, John Doe,G building,05-Aug-2012,08-Aug-2012,New York City,ABC2345,UCD2 +3467, John Moe,H building,05-Aug-2012,08-Aug-2012,New York City,DEF2345,UCD8 +0645, John Slo,I building,05-Aug-2012,08-Aug-2012,New York City,GHI2345,UCD7 +6765, John Hor,j building,05-Aug-2012,08-Aug-2012,New York City,JKL2345,UCD8 +7111,
Update Here is what the contents of the error file look likeforeach my $records(@merged_array){ if($records =~ /$sample_ids[3]/){ print $records; }
Just to check that the array @sample_ids contains the actual ids I printed the contents of the sample id' and got the following************************************** Errors for file: Merged_CSVfiles_1.txt ************************************** Sample_identifier 11SS00342 / IRD Clinic clinic_HIV000140180: * The following fields contain invalid values: Blah blah blah blah * Sample_identifier 11SS00336 / IRD Clinic clinic_HIV000140174: * The following fields contain invalid values: Yada yada yada * Sample_identifier 11SS00303 / IRD Clinic clinic_HIV000140141: * The following fields contain invalid values: yeah yeah yeah
I get the following output which is the list of the ID's (this is just a truncated list, just 8 of the 438 errors) UCD11-02580-V UCD11-02581-P UCD11-02581-V UCD11-02583-P UCD11-02583-V UCD11-02584-P UCD11-02584-V UCD11-02585-P I also did the following as a checkpush(@sample_ids,$line); print(@sample_ids);
and I got 11SS00304push(@sample_ids,$line); }# End of the if loop }#End of the for-each loop print $sample_ids[3];
|
|---|