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

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

Replies are listed 'Best First'.
Re^3: Why is the following instance of Matching using an array element not working
by Kenosis (Priest) on Aug 10, 2012 at 20:31 UTC

    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

        The script below is based upon the Text::CSV documentation:

        use Modern::Perl; use Text::CSV; my @Sample_Ids_m; my $csv = Text::CSV->new( { binary => 1 } ) # should set binary att +ribute. or die "Cannot use CSV: " . Text::CSV->error_diag(); while ( my $row = $csv->getline(*DATA) ) { push @Sample_Ids_m, $row->[6]; # Element 6 is the ID } say for @Sample_Ids_m; # Print the collected I +Ds __DATA__ John Doe,G building,05-Aug-2012,08-Aug-2012,"New York City, NY",ABC234 +5,UCD23467, 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,

        Output:

        UCD23467 UCD80645 UCD76765 UCD87111

        Adapted for your csv file (untested):

        use Modern::Perl; use Text::CSV; my @Sample_Ids_m; my $csv = Text::CSV->new( { binary => 1 } ) # should set binary att +ribute. or die "Cannot use CSV: " . Text::CSV->error_diag(); open my $fh, "<", "Merged_CSVfiles.csv" or die "Merged_CSVfiles.csv: $ +!"; while ( my $row = $csv->getline($fh) ) { push @Sample_Ids_m, $row->[16]; # Element 16 is the ID } $csv->eof or $csv->error_diag(); close $fh; say for @Sample_Ids_m; # Print the collected I +Ds

        From your code above, it looks like the ID is the 16th element in @columns, and the $row->[16] notation grabs it after parsing a csv line. See if this may assist your scripting...