Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

Re: reading file

by tobyink (Abbot)
on Nov 29, 2012 at 11:15 UTC ( #1006232=note: print w/ replies, xml ) Need Help??


in reply to reading file

Here's a fun parsing example though...

use MooX::Struct Record => [qw( $id $et @ac $os $pt @rs @ra )], Person => [qw( $surname $initials )], ; use Data::Dumper; my %IS_PERSON = ( ra => 1, ); my %IS_LIST = ( ac => 1, rs => 1, ra => 1, ); my %record; my @records; while (<DATA>) { chomp; my ($field, $value) = /^(..)\s*(.+)$/; $field = lc $field; if ($field eq 'id' and keys %record) { push @records, Record->new(%record); %record = (); # start new record } if ($IS_LIST{$field}) { push @{$record{$field}}, map { $IS_PERSON{$field} ? Person[split] : $_ } split m{,\s*}, $value; } else { $record{$field} = $IS_PERSON{$field} ? Person[split / /, $valu +e] : $value; } } # EOF, push last record push @records, Record->new(%record); print $records[1]->ra->[0]->surname; __DATA__ ID AaaI ET R2 AC RB00001; OS Acetobacter aceti ss aceti PT XmaIII RS CGGCCG, 1; RA Tagami H., Tayama K., Tohyama T., Fukaya M., Okumura H., Kawamura + Y., RA Horinouchi S., Beppu T.; ID AaaII ET R2 AC RB00001; OS Acetobacter aceti ss aceti PT XmaIII RS CGGCCG, 1; RA Horinouchi S., Beppu T.; ID AaaIII ET R2 AC RB00001; OS Acetobacter aceti ss aceti PT XmaIII RA Horinouchi S., Beppu T.; RS CGGCCG, 1;
perl -E'sub Monkey::do{say$_,for@_,do{($monkey=[caller(0)]->[3])=~s{::}{ }and$monkey}}"Monkey say"->Monkey::do'


Comment on Re: reading file
Download Code

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (11)
As of 2014-07-11 21:05 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    When choosing user names for websites, I prefer to use:








    Results (235 votes), past polls