Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
Thats a very common problem, you are trying to add a record only when the successor is to be parsed, but the last record has no successor (sic ;)!

Most people try to solve by repeating code to add the last record after the loop.

But it's much cleaner this way (avoiding a posteriori state logic)

use strict; use warnings; use Data::Dump; my $header; my %sequence; while ( my $line = <DATA> ){ chomp $line; if ( $line =~ /^>(.*)/ ) { $header = $1; } else { $sequence{$header} .= $line; } } dd \%sequence; __DATA__ >sequence_5849 CCTGCGGAAGATCGGCACTAGAATAGCCAGAACCGTTTCTCTGAGGCTTCCGGCCTTCCC TCCCACTAATAATTCTGAGG >sequence_5959 CCATCGGTAGCGCATCCTTAGTCCAATTAAGTCCCTATCCAGGCGCTCCGCCGAAGGTCT ATATCCATTTGTCAGCAGACACGC >sequence_0808 CCACCCTCGTGGTATGGCTAGGCATTCAGGAACCGGAGAACGCTTCAGACCAGCCCGGAC TGGGAACCTGCGGGCAGTAGGTGGAAT
output
{ sequence_0808 => "CCACCCTCGTGGTATGGCTAGGCATTCAGGAACCGGAGAACGCTTCAGAC +CAGCCCGGACTGGGAACCTGCGGGCAGTAGGTGGAAT", sequence_5849 => "CCTGCGGAAGATCGGCACTAGAATAGCCAGAACCGTTTCTCTGAGGCTTC +CGGCCTTCCCTCCCACTAATAATTCTGAGG", sequence_5959 => "CCATCGGTAGCGCATCCTTAGTCCAATTAAGTCCCTATCCAGGCGCTCCG +CCGAAGGTCTATATCCATTTGTCAGCAGACACGC", }

Cheers Rolf

( addicted to the Perl Programming Language)

update

a *general pattern* to solve such problems while staying DRY is to use references

if ( $line =~ /^(HEAD_PATTERN)/ ) { $data = \ $deeply{nested}{structure}{$1}; # reference data + } else { $$data .= $line; # derefrence data }

like this you don't need to repeat the path of a deeply nested data structure, which might vary in multiple dimensions

update
added some explanation

In reply to Re^3: Reading file into a hash (last record problem) by LanX
in thread Reading file into a hash by PerlSufi

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 having a coffee break in the Monastery: (2)
As of 2024-04-16 23:39 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found