Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

Re: splitting data advice requested

by kennethk (Abbot)
on May 13, 2009 at 14:44 UTC ( [id://763781]=note: print w/replies, xml ) Need Help??


in reply to splitting data advice requested

Combining bloodnok's suggestion for a hash with almut's suggestion for an approach to splitting combined with split's limit argument and a positive look-ahead assertion, I give you:

use strict; use warnings; local $/; my %hash = map split(/\n/, $_, 2), split /\n(?=>)/, <DATA>; s/\n//g foreach values %hash; __DATA__ >SEQ1 -----I--RL--AAIDVDG-NLT----------D--R--D-RL-ISTKA-IESIRS--A- -E-K--------K-GLT-VSL----LS------GN-V----I-PVV---YA-L------K IF---------------L-----GINGPVF------------------------------ >SEQ2 -MKI----KA--ISIDIDG-TIT------YPN-R-------MIHEK--A-LEAIRR--A- -E-S--------L-GIP-IML----VT------GN-T----V-QFA---EA-A------S IL---------------I-----G----TS-----------------GP-VV-------- >SEQ3 --KI----KA--ISIDIDG-TIT------YPN-R-------MIHEK--A-LEAIRR--A- -E-S--------L-GIP-IML----VT------GN-T----V-QFA---EA-A------S IL---------------I-----G----TS-----------------GP-VV-------- ---AE--D------GG---A---------------------------------------I

Replies are listed 'Best First'.
Re^2: splitting data advice requested
by johngg (Canon) on May 13, 2009 at 15:45 UTC

    Another way of getting rid of the newlines would be to lose all of them with the second split, passing the fields out in an anonymous array, and then map out the key and sequence using shift and join.

    use strict; use warnings; use Data::Dumper; my %hash = map { ( shift @$_, join q{}, @$_ ) } map { [ split m{\n} ] } map { split m{\n(?=>)} } do { local $/; <DATA> }; print Data::Dumper->Dumpxs( [ \ %hash ], [ qw{ *hash } ] ); __DATA__ >SEQ1 -----I--RL--AAIDVDG-NLT----------D--R--D-RL-ISTKA-IESIRS--A- -E-K--------K-GLT-VSL----LS------GN-V----I-PVV---YA-L------K IF---------------L-----GINGPVF------------------------------ >SEQ2 -MKI----KA--ISIDIDG-TIT------YPN-R-------MIHEK--A-LEAIRR--A- -E-S--------L-GIP-IML----VT------GN-T----V-QFA---EA-A------S IL---------------I-----G----TS-----------------GP-VV-------- >SEQ3 --KI----KA--ISIDIDG-TIT------YPN-R-------MIHEK--A-LEAIRR--A- -E-S--------L-GIP-IML----VT------GN-T----V-QFA---EA-A------S IL---------------I-----G----TS-----------------GP-VV-------- ---AE--D------GG---A---------------------------------------I

    The output.

    %hash = ( '>SEQ1' => '-----I--RL--AAIDVDG-NLT----------D--R--D-RL-ISTK +A-IESIRS--A--E-K--------K-GLT-VSL----LS------GN-V----I-PVV---YA-L---- +--KIF---------------L-----GINGPVF------------------------------', '>SEQ3' => '--KI----KA--ISIDIDG-TIT------YPN-R-------MIHEK-- +A-LEAIRR--A--E-S--------L-GIP-IML----VT------GN-T----V-QFA---EA-A---- +--SIL---------------I-----G----TS-----------------GP-VV-----------AE- +-D------GG---A---------------------------------------I', '>SEQ2' => '-MKI----KA--ISIDIDG-TIT------YPN-R-------MIHEK-- +A-LEAIRR--A--E-S--------L-GIP-IML----VT------GN-T----V-QFA---EA-A---- +--SIL---------------I-----G----TS-----------------GP-VV--------' );

    I hope this is of interest.

    Cheers,

    JohnGG

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://763781]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others imbibing at the Monastery: (6)
As of 2024-04-23 15:46 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found