ashnator has asked for the wisdom of the Perl Monks concerning the following question:
Dear Monks,
Hi I have written a program using hash of hashes type of data structure. I am using the keys in File 1 to parse the file 2.
I am unable to get the correct output. I have got many multiple values for the single key in my File 1.
I am getting this error / warning in my program
Here is my codeUse of uninitialized value in hash element at TEST.pl line 17, <$fh> l +ine 4. Use of uninitialized value in hash element at TEST.pl line 18, <$fh> l +ine 4. Use of uninitialized value in hash element at TEST.pl line 17, <$fh> l +ine 5. Use of uninitialized value in hash element at TEST.pl line 20, <$fh> l +ine 5. KEY in File1 not found in File2 KEY in File1 not found in File2 KEY 155369268: File2 string length too short for File1 position value KEY 155369268: File2 string length too short for File1 position value KEY 269212605: File2 string length too short for File1 position value
My Input file looks like this:-#!/usr/bin/perl use strict; use warnings; my $qfn1 = "File1.txt"; my $qfn2 = "File2.txt"; my %positions; { open(my $fh, '<', $qfn1) or die("Cannot open file \"$qfn1\": $!\n"); while (<$fh>) { chomp; my ($key, $pos) = split /\s+/; if (!$positions{$key}) { $positions{$key} = [$pos]; } else { my $ref = $positions{$key}; push @$ref,$pos; } } } my %sequences; { open(my $fh, '<', $qfn2) or die("Cannot open file \"$qfn2\": $!\n"); my $key; while (<$fh>) { if ( s/^>// ) { $key = ( split /\|/ )[1]; } else { chomp; $sequences{$key} .= $_; } } } for my $key ( sort keys %positions ) { my $ref = $positions{$key}; foreach my $value (@$ref) { if ( ! exists( $sequences{$key} )) { warn "KEY $key in File1 not found in File2\n"; next; } if ( length( $sequences{$key} ) < $positions{$key} ) { warn "KEY $key: File2 string length too short for File1 positi +on value\n"; next; } my $index = rindex( $sequences{$key}, "ATG", $positions{$key} ); if ( $index < 0 ) { warn sprintf( "KEY %s: No ATG in File2 string prior to positio +n %d\n", $key, $positions{$key} ); next; } $index += 3 while ( ($index + 3) < $positions{$key} ); print "$key $positions{$key} " . substr($sequences{$key}, $index, +3) . "\n"; } }
I want my Output should be like this:-File 1:- 155369268 17 A 155369268 70 G 269212605 75 T File 2:- >gi|155369268|ref|NM_001100917.1| some text AAACAATGTCGATTCTATGATGCGAACGCAGCATTTCAGGGACTGGAATGGGAGCTTACGGTTTTTTACG ACAGAATCATCAATATCTTGGAAGAAAAAGAATGTTAAGAAATAACAAAACAATAATTATTAAGTACTTT >gi|269212605|ref|XM_401716884.1| some text AGACAAGCTTGTCCTGATGTTCCTTGCCCTGGCAGATGTTCAGGACCTTCCTTTGATTCAACCCTATGAC CTAATTGGCCCAAGCTTTCGGGGCTGTCATTGTCTGTTTGTCATTCAAGGGCCCAAGCTGAAGAGGGGGT
good bye155369268 17 CTA 155369268 70 CGA 269212605 120 TTG
20081224 Janitored by Corion: Restored content
|
---|
Back to
Seekers of Perl Wisdom