in reply to Re^2: 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

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.

  • Comment on Re^3: Why is the following instance of Matching using an array element not working

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

    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...

        Thanks again Kenosis and thanks to everyone who replied. I was able to fix my program finally. I made a lot of modifications from my original program. Using the CSV module was a much better strategy for getting out the total number of sample_ids. I did find it very helpful to go back and check that all my arrays and hashes were storing what they in fact were storing.Thanks again for all your suggestion

        I made a hash table with the sample_Id as keys and the line of text from the Merged_File with the sample id as a value. I used dumpvalue to display my hash contents and used a hash slice to print out the specific values I needed. Thanks to everyone dave

        use Dumpvalue; %id_lookup_table = map { ($Merged_Ids[$_], $merged_array[$_]) } 0..$#M +erged_Ids; my $dumper = Dumpvalue -> new; $dumper -> dumpValue(\%id_lookup_table); open(ERRORREPORT,"+<","reported_list.csv") or die "reported_li +st.csv: $!"; print ERRORREPORT @id_lookup_table{(@sample_ids)}; close ERRORREPORT; 'UCD12-00242-P' => "\"Joe Schmie\",ABD032,12-Feb-12,23-Feb-12,\"Whale +Tail Island, Fictitious Place\",USA,California,etc