Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
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
Use 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
Here is my code
#!/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"; } }
My Input file looks 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
I want my Output should be like this:-
155369268 17 CTA 155369268 70 CGA 269212605 120 TTG
good bye

20081224 Janitored by Corion: Restored content


In reply to Hashs of hash (multiple value) key problem. by ashnator

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others surveying the Monastery: (9)
As of 2024-04-24 07:16 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found