I've found the other solutions quite illuminating, but not wanting to be left out (and because I already did it ;-), here's my kick at it. The patterns could use some loosening on whitespace, I'm hoping no objects have missing attributes (you'd get the previous ones), and you can't have an object nested inside another with this, but it appears sufficient for the sample data. Hmm... I'm also ignoring the extraction of entire objects that tilly handles so well. Now I'll go read up on Parse::RecDecent - so many monks can't be wrong!

use strict; my %object; open IN, '<'.pop or die "can't open data: $!\n"; until(<IN> =~ /^object\s*\{/) {}; #skip ahead to the body of the fir +st object{} my($id, $origin, $dims); while(<IN>) { SWITCH: { /id "(.+)"/ && do { $id = $1; last SWITCH; }; /origin \{ (\d+) (\d+) \}/ && do { $origin = "$1,$2"; last SWITCH; }; /dimensions \{ (\d+) (\d+) \}/ && do { $dims = "$1,$2"; last SWITCH; }; (/^object\s*\{/ || eof(IN)) && do { $object{$id}{'origin'} = + $origin; $object{$id}{'dims'} = + $dims; }; # default: on to the next line }#SWITCH } foreach my $id (keys %object) { print "$id: "; foreach my $attr (keys %{$object{$id}} ) { print "$attr = $object{$id}{$attr} "; } print "\n"; }
produces:
DAILY.evo: origin = 0,0 dims = 9360,130 1400.cm1: origin = 0,130 dims = 900,1

--
I'd like to be able to assign to an luser


In reply to Re: Parsing a multiline data structure by Albannach
in thread Parsing a multiline data structure by HamNRye

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.