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

Re: Extraction help.

by philiprbrenan (Monk)
on Sep 02, 2012 at 22:45 UTC ( #991334=note: print w/ replies, xml ) Need Help??


in reply to Extraction help.

Apologies in advance for the multiple posts on this subject, but I discovered that I wanted to illustrate what is, for me, one of Perl's most compelling features: the transmogrification of Text into Objects.

use feature ":5.14"; use warnings FATAL => qw(all); use strict; use Data::Dump qw(dump pp); my @d = split /\n/, <<'END'; O59734 | Name=smf1; ORFNames=SPBC3E7.14, SPBC4F6.01 Q97W02 | Name=dbh; Synonyms=dpo4; OrderedLocusNames=SSO2448 B0JTM2 | Name=trpC; OrderedLocusNames=MAE_45030 Q0WVE9; Q5XF02; Q9ZVN7 | OrderedLocusNames=At1g05030; ORFNames=T7A14.1 +0 Q15X31 | Name=rraB; OrderedLocusNames=Patl_1031 Q66640 | Name=36 Q9F2S0 | Name=hemL; OrderedLocusNames=SCO4469; ORFNames=SCD65.12 A9R5H1 | Name=dctA; OrderedLocusNames=YpAngola_A4067 Q7N3W0 | Name=rnt; OrderedLocusNames=plu2603 Q6GNW0 | Name=lrrfip2 Q4L4T4 | OrderedLocusNames=SH2032 B7I359 | Name=rplL; OrderedLocusNames=AB57_0368 B2HII2 | Name=leuD; OrderedLocusNames=MMAR_1727 END sub Codes(@) # Database {package Codes; our $D = bless {}; for(@_) # Parse data {my ($c, $d) = split /\s+\|\s+/; for(split /;\s+/, $d) {my ($k, $v) = split /=/; $D->{$_}{$k} = $v for split /;\s+/, $c; } } sub find($$) # Find field {my ($field, $code) = @_; $D->{$code} ? $D->{$code}{$field} : undef } eval 'sub '.$_.'{find("'.$_.'", $_[1])}' # Accessor functions for keys %{{map({($_, 1)} map({keys %$_} values %$D))}}; $D } my $D = Codes(@d); say $D->ORFNames('Q9ZVN7'); say $D->Name('B2HII2'); say $D->OrderedLocusNames('B2HII2');

Produces

T7A14.10 leuD MMAR_1727


Comment on Re: Extraction help.
Select or Download Code

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others avoiding work at the Monastery: (7)
As of 2014-12-21 09:22 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?





    Results (104 votes), past polls