When reading "File1.txt" you are not retaining the location information for each record. You could use a hash of hashes structure to do that. In the code below you can see this and I have added Data::Dumper output so that you can visualise the structure.

There seems to be some confusion in your code as to how to find the position for replacement. It would appear that you read the postition and new location from "File1.txt" but you then try to find things using rindex and then record the $current_base even though you don't seem to use it and are replacing it anyway. My code below just does a replacement based on the information in "File1.txt".

You process the records one at a time so I'm not sure why you use the %sequences hash. Why not just use a simple scalar to hold the record? I am assuming that the format you have posted for "File2.txt" is accurate with the data being broken into lines of 70 characters. Finding the correct position for the replacement needs to allow for the line breaks.

The code below inlines the input files using heredocs and the output file by opening a filehandle on a scalar reference. This is just to keep everything in the script but you can simply relace those constructs with actual files. I have changed the locations to asterisks just so they stand out in the output file so you can see the replacement happening. I have added a record where the replacement is not on the first line of data and another record where no change takes place.

use strict; use warnings; use Data::Dumper; open my $in1FH, q{<}, \ <<END_File1 or die qq{open: < "File1.txt": $!\ +n}; 155369268 5 * 169212695 7 * 175387629 127 * END_File1 my %positions = (); while( <$in1FH> ) { my( $key, $posn, $locn ) = split; $positions{ $key } = { position => $posn, location => $locn, }; } close $in1FH or die qq{close: < "File1.txt": $!\n}; print Data::Dumper->Dumpxs( [ \ %positions ], [ qw{ *positions } ] ); open my $in2FH, q{<}, \ <<END_File2 or die qq{open: < "File2.txt": $!\ +n}; >gi|155369268|ref|NM_001100917.1| After all AAACAATGTCGATTCTATGATGCGAACGCAGCATTTCAGGGACTGGATGAGGAGCTTACGGTTTTTTACT ACAGAATCATCAATATCTTGGAAGAAAAAGAATGTTAAGAAATAACAAAACAATAATTATTAAGTACTTT >gi|169212695|ref|XM_001716884.1| Its the code AGACAAGCTTGTCCTGATGTTCCTTGCCCTGGCAGATGTTCAGGACCTTCCTTTGATTCAACCCCTCCAC CTAAATGGCCCAAGCTTTCGGGGCTGTCATTGTCTGTTTGTCATTCAAGGGCCCAAGCTGAAGAGGGGGT >gi|175387629|ref|PM_001716884.1| Its the code again AGACAAGCTTGTCCTGATGTTCCTTGCCCTGGCAGATGTTCAGGACCTTCCTTTGATTCAACCCCTCCAC CTAAATGGCCCAAGCTTTCGGGGCTGTCATTGTCTGTTTGTCATTCAAGGGCCCAAGCTGAAGAGGGGGT AAGCTTTCTAAATGGCCCCGGGGCTTTGTCATTCAAGGGTGTGCCCAAGCCATTGTCTTGAAGAGGGGGT CGGGGCTTTTCAAGGGTCATTGTGCCCAAG >gi|192669295|ref|XM_001716884.1| Its the code AGACAAGCTTGTCCTGATGTTCCTTGCCCTGGCAGATGTTCAGGACCTTCCTTTGATTCAACCCCTCCAC CTAAATGGCCCAAGCTTTCGGGGCTGTCATTGTCTGTTTGTCATTCAAGGGCCCAAGCTGAAGAGGGGGT END_File2 # 1 2 3 4 5 6 7 #234567890123456789012345678901234567890123456789012345678901234567890 my $rsFile2out = \ do{ my $dummy }; open my $outFH, q{>}, $rsFile2out or die qq{open: > "File2out.txt": $!\n}; my $record = <$in2FH>; while( <$in2FH> ) { if( m{^>gi} ) { processRecord( $record ); $record = $_; } else { $record .= $_; } } processRecord( $record ); close $in2FH or die qq{close: < "File2.txt": $!\n}; close $outFH or die qq{close: > "File2out.txt": $!\n}; print ${ $rsFile2out }; sub processRecord { my $record = shift; my $key = ( split m{\|}, $record )[ 1 ]; if( exists $positions{ $key } ) { my( $header, @data ) = split m{\n}, $record; my $dataStr = join q{}, @data; substr $dataStr, $positions{ $key }->{ position } - 1, 1, $positions{ $key }->{ location }; print $outFH join qq{\n}, $header, unpack( q{(a70)*}, $dataStr ), q{}; } else { print $outFH $record; } return; }

The output.

%positions = ( '175387629' => { 'location' => '*', 'position' => '127' }, '169212695' => { 'location' => '*', 'position' => '7' }, '155369268' => { 'location' => '*', 'position' => '5' } ); >gi|155369268|ref|NM_001100917.1| After all AAAC*ATGTCGATTCTATGATGCGAACGCAGCATTTCAGGGACTGGATGAGGAGCTTACGGTTTTTTACT ACAGAATCATCAATATCTTGGAAGAAAAAGAATGTTAAGAAATAACAAAACAATAATTATTAAGTACTTT >gi|169212695|ref|XM_001716884.1| Its the code AGACAA*CTTGTCCTGATGTTCCTTGCCCTGGCAGATGTTCAGGACCTTCCTTTGATTCAACCCCTCCAC CTAAATGGCCCAAGCTTTCGGGGCTGTCATTGTCTGTTTGTCATTCAAGGGCCCAAGCTGAAGAGGGGGT >gi|175387629|ref|PM_001716884.1| Its the code again AGACAAGCTTGTCCTGATGTTCCTTGCCCTGGCAGATGTTCAGGACCTTCCTTTGATTCAACCCCTCCAC CTAAATGGCCCAAGCTTTCGGGGCTGTCATTGTCTGTTTGTCATTCAAGGGCCCAA*CTGAAGAGGGGGT AAGCTTTCTAAATGGCCCCGGGGCTTTGTCATTCAAGGGTGTGCCCAAGCCATTGTCTTGAAGAGGGGGT CGGGGCTTTTCAAGGGTCATTGTGCCCAAG >gi|192669295|ref|XM_001716884.1| Its the code AGACAAGCTTGTCCTGATGTTCCTTGCCCTGGCAGATGTTCAGGACCTTCCTTTGATTCAACCCCTCCAC CTAAATGGCCCAAGCTTTCGGGGCTGTCATTGTCTGTTTGTCATTCAAGGGCCCAAGCTGAAGAGGGGGT

I hope this is helpful.

Cheers,

JohnGG

Update: Fixed issues with TABs in posted code mucking up indentation.


In reply to Re: Character replacement by johngg
in thread Character replacement by ashnator

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



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.