Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine
 
PerlMonks  

Re: reading file

by tobyink (Canon)
on Nov 29, 2012 at 11:15 UTC ( [id://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'

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (5)
As of 2024-04-19 22:36 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found