in reply to Modelling a data structure

What you need is a configuration file to ehh ... configure your program.

Something like YAML seems well-suited for your means. The configuration in a YAML file will translate directly to your datastructure.

CountZero

"If you have four groups working on a compiler, you'll get a 4-pass compiler." - Conway's Law

Replies are listed 'Best First'.
Re^2: Modelling a data structure
by sch (Pilgrim) on Feb 07, 2007 at 16:55 UTC

    Plan B was going to be to go with an XML config file, but given that once this is setup it won't get changed very often.

    But thanks for the suggestion.

    Simon

      Plan B was going to be to go with an XML config file...

      You mean something like this?

      $ cat test.xml <?xml version="1.0"?> <filelist> <file name="file1"> <field name="field1" start="50" length="20"/> <field name="field2" start="80" length="20"/> <field name="field3" start="100" length="20"/> </file> <file name="file2"> <field name="field1" start="52" length="22"/> <field name="field2" start="82" length="22"/> <field name="field3" start="120" length="22"/> </file> <file name="file3"> <field name="field1" start="53" length="23"/> <field name="field2" start="83" length="23"/> <field name="field3" start="130" length="23"/> </file> </filelist> $ perl -MXML::Simple -MData::Dumper -e '$x=XMLin("test.xml"); print Du +mper($x)' $VAR1 = { 'file' => { 'file2' => { 'field' => { 'field1' => { 'length' => '22' +, 'start' => '52' }, 'field2' => { 'length' => '22' +, 'start' => '82' }, 'field3' => { 'length' => '22' +, 'start' => '120' } } }, 'file1' => { 'field' => { 'field1' => { 'length' => '20' +, 'start' => '50' }, 'field2' => { 'length' => '20' +, 'start' => '80' }, 'field3' => { 'length' => '20' +, 'start' => '100' } } }, 'file3' => { 'field' => { 'field1' => { 'length' => '23' +, 'start' => '53' }, 'field2' => { 'length' => '23' +, 'start' => '83' }, 'field3' => { 'length' => '23' +, 'start' => '130' } } } } };
      But... what? Looks pretty simple to me. I'd rather not have all those data values hard-coded in the perl script itself, and reading them from a basic xml file seems like a no-brainer.

      I guess it's a little goofy that there are two layers in XML::Simple's "default" hash structure that are sort of useless ("file" and "field"), but if you tried a different way of structuring the xml (and/or spent some time with the XML::Simple man page), you could probably improve on that. (Or you could just use the default as-is and get done quicker.)