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.

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,
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.
foreach my $records(@merged_array){ if($records =~ /$sample_ids[3]/){ print $records; }
Update Here is what the contents of the error file look like
************************************** 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
Just to check that the array @sample_ids contains the actual ids I printed the contents of the sample id' and got the following
push(@sample_ids,$line); print(@sample_ids);
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 check
push(@sample_ids,$line); }# End of the if loop }#End of the for-each loop print $sample_ids[3];
and I got 11SS00304

In reply to Why is the following instance of Matching using an array element not working by MyJeweledPerls

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.