in reply to Why is the following instance of Matching using an array element not working

It looks like your iterating over lines from Errors-email.txt (and it would have helped to see some of these lines, since they contain the IDs):

... open (ERRORFILE,"<", "Errors-email.txt") || die "File not found\n"; @error_array = <ERRORFILE>; ... foreach my $line(@error_array){ # # Some $line substitutions here... ... push(@sample_ids,$line); ...

Then you have the following:

... foreach my $records(@merged_array){ if($records =~ /$sample_ids[3]/){ ...

Where @merged_array contains lines from your cvs file.

Does @sample_ids contain only the IDs? Did you try printing $sample_ids[3] to see what you get? Since you successfully used UCD67832 in the regex, it's evident that $sample_ids[3] does not equal UCD67832. What does it equal? Is UCD67832 a different element of @sample_ids? Does @sample_ids comtain whole lines instead of IDs?

I can't see that you've actually isolated the ID (unless it's via the set of substitutions), so I'd tend to focus on grabbing the ID from $line around those substitutions, and then push(@sample_ids,$id);

Hope this helps!

Replies are listed 'Best First'.
Re^2: Why is the following instance of Matching using an array element not working
by MyJeweledPerls (Initiate) on Aug 10, 2012 at 19:33 UTC

    Thanks a lot for this post. I went back and checked

    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

    What I did realized though was that:
    foreach my $records (@merged_array) { my @elements = split /,/, $records; print $elements[0];

    The Output from this was: "John Doe"John Doe"John Doe"John Doe"John Doe

    I realized that some of the fields themselves had commas within and that I couldn't consistently get the field I wanted by "print18" in this case

      You're most welcome, MyJeweledPerls!

      If getting the field you want from the cvs lines is still presenting a problem, consider using Text::CSV to open and parse your cvs file. Its documentation shows how to do this, so you can get that field.

        I have worked with Text::CSV in the past. I have only slight issue with it, which I am not sure if I am not getting how to use it correctly. Here is the following code:

        my $csv = Text::CSV->new(); open (CSV,"<", "Merged_CSVfiles.csv") || die "File not found\n"; while (<CSV>) { # While Loop Open Bracket next if ($. == 1); if ($csv->parse($_)) { #If Open Bracket my @columns = $csv->fields(); #__ Assigning $columns[0]-$columns[26] to variables my @Sample_Ids_m = $columns[16]; print ($Sample_Ids_m[0]); } else { # If Closed Bracket # Else Open Bracket my $err = $csv-> error_input; print "Failed to parse line: $err"; close CSV; } # Closed Else Bracket }#Closed While Loop Bracket

        All the ID are loaded into the first element of the array @Sample_Ids_m. This would be a problem but arrays can be different lengths and there are no rules current for constructing Sample ID numbers. It is a random sort of thing.

        If there was some uniformity I could use the substr function and a for loop and extract our individual id's but I dont seem to be able too. I will play around with it for a bit more