Re: Parsing multi-line text



On 2008-02-18 10:42, keith@xxxxxxxxxxxxxxxxxx <keith@xxxxxxxxxxxxxxxxxx> wrote:
I have a data file structured something like this:

------------------8<-----------------------
Chunk 01
NAME: "Alice"
Description: "Some other string"
Age: 37
Chunk 02
NAME: "Bob"
Description: "Some other string"
Age: 28
Chunk 03
FIRST: "Carol"
Description: "Some other string"
Age: 32
Chunk 04
FIRST: "Dave"
Description: "Some other string"
Age: 22
------------------8<-----------------------

and I want to extract from it to produce output something like this:

------------------8<-----------------------
01 NAME: Alice -> 37
02 NAME: Bob -> 28
03 NAME: Carol -> 32
04 NAME: Dave -> 22
------------------8<-----------------------

Sure about that? In the input you have sometimes "FIRST" and sometimes
"NAME", but in the output it is always NAME. Assuming this is
intentional:


#!/usr/bin/perl
use strict;
use warnings;

my $s = <<EOS;
Chunk 01
NAME: "Alice"
Description: "Some other string"
Age: 37
Chunk 02
NAME: "Bob"
Description: "Some other string"
Age: 28
Chunk 03
FIRST: "Carol"
Description: "Some other string"
Age: 32
Chunk 04
FIRST: "Dave"
Description: "Some other string"
Age: 22
EOS

while ($s =~ m{
^Chunk \s (\d+) \n
\s+(NAME|FIRST): \s "(.*?)" \n
\s+Description: \s "(.*?)" \n
\s+Age: \s (\d+) \n
}xmg
) {
print "$1 NAME: $3 -> $5\n";
}

hp
.



Relevant Pages

  • Parsing multi-line text
    ... Chunk 01 ... Description: "Some other string" ... Age: 37 ... 02 NAME: Bob -> 28 ...
    (comp.lang.perl.misc)
  • Re: Parsing multi-line text
    ... Chunk 01 ... Description: "Some other string" ... Age: 37 ... 03 NAME: Carol -> 32 ...
    (comp.lang.perl.misc)
  • Re: using Line Input
    ... string in the next chunk. ... Private Sub Class_Initialize ... Public Function ReadDelineatedLine() As String ...
    (microsoft.public.vb.general.discussion)
  • Re: IF statement
    ... StrSize as String ... That would work if the Age was a string field and was never null. ... AgeCate = "Don't Know" ...
    (microsoft.public.access.queries)
  • Re: "Read stuff from a file and chop it up to do stuff" code advice wanted.
    ... had a character set without control characters, ... ;; Throw out the control character and return the chunk. ... ;; and then run my code on the text string that this code creates. ... Well, first of all, NTH is very inefficient for lists. ...
    (comp.lang.lisp)