http://www.perlmonks.org?node_id=960447


in reply to Does the output exist in a hash or array?

I would maintain a hash of samples you have already seen. That way you could check that hash to see if you've already printed that sample before or not.

I'm a little uncertain though. Do you want only one result per sample? Or any unique sample/tab3/tab23 combination? The following assumes that you only want one result per sample...

my %seen; while (<BATCH>) { chomp; s/\r//; my $filename = $_; $out_name = $filename; $out_name =~ s/\.txt//; $out_dir_name = "apt_SNPCall_".$out_name; foreach my $lf (@list_files){ open (SAMPLE, "$out_dir_name/birdseed-v2.report.txt") || die "Erro +r to open: $!"; while (<SAMPLE>) { chomp; my @tab = split("\t", $_); if ($tab[0] eq $lf){ my $sample = $lf; $sample =~ s/\.CEL//; if (!exists $seen{$sample}) { print $sample."\t".$tab[2]."\t".$tab[22]."\n"; $seen{$sample} = 1; } } } } }

Replies are listed 'Best First'.
Re^2: Does the output exist in a hash or array?
by Light (Initiate) on Mar 20, 2012 at 09:57 UTC

    I only want the first result of the sample, so if the sample already exists in the output I guess, I want to skip that sample and go to the next line.

    Thanks for your help.