Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

Re: Parsing a Tagged File Format

by Anonymous Monk
on Apr 30, 2003 at 10:41 UTC ( [id://254239]=note: print w/replies, xml ) Need Help??


in reply to Parsing a Tagged File Format

#! perl -slw use strict; use Data::Dumper; my %data; my $current_key; while( <DATA> ) { my ($key, $value) = m[(^[\w]{2})?\s+(.*$)]; if( defined $key ) { last if $key eq 'ER'; push @{ $data{$key} }, $value; $current_key = $key; } else { push @{ $data{$current_key} }, $value; } } print Dumper \%data __DATA__ T1 Line1 T2 Line2 Line3 Line4 T3 Line5 Line6 ER

Replies are listed 'Best First'.
Re: Re: Parsing a Tagged File Format
by runrig (Abbot) on Apr 30, 2003 at 18:36 UTC
    Basically the same answer, but a little more golfed:
    use strict; use Data::Dumper; my %data; my $current_key; while( <DATA> ) { my ($key, $value) = m[^((?:\w{2})?)\s+(.*)] or die "Bad line: $_"; last if $key eq 'ER'; $current_key = $key || $current_key; die "No key defined" unless $current_key; push @{ $data{$current_key} }, $value; } print Dumper \%data __DATA__ T1 Line1 T2 Line2 Line3 Line4 T3 Line5 Line6 ER
    Updated. I should test these things first :-) (and its silly that (\w{2}?) doesn't DWIM) (and see Aristotle's answer further down for a very similar answer). Oh well).

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others about the Monastery: (9)
As of 2024-04-23 11:05 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found