in reply to Tricky Parsing Ko'an
There's too much missing information for a complete and tested solution, and if your 'structure' thingies can be nested, then this probably won't help, but on the basis of what you've posted this seems to come pretty close to your requirements:
#! perl -slw use strict; use Data::Dumper; my $re = qr[ (string|float|float2|float3|float4) \s+ (\w+) \s* (?: < \s* ( [^>]+? ) \s* > \s* )? = \s* ( \S+ ) \s* ; ]x; my $data = do{ local $/; <DATA> }; my %vars; while( $data =~ m[$re]smg ) { my( $type, $name, $structure, $value ) = ( $1, $2, $3, $4 ); $vars{ $name } = { type => $type, value => $value }; if( defined $structure ) { while( $structure =~ m[$re]smg ) { my( $stype, $sname, $svalue ) = ( $1, $2, $4 ); if( my( $prefix, $rest ) = $sname =~ m[([A-Z]+)([A-Z][a-z] ++)] ) { $vars{ $name }{ $prefix }{ $rest } = $svalue; } } } } print Dumper \%vars; __DATA__ float myVarA < float UIMin = 1; float UIMax = 0; float UIStep = .001; string UIWidget = "slider"; > = 0.5f; float myVarB = 1.0; float4 myVarC = {1,0,0,1};
It produces:
C:\test>junk2 $VAR1 = { 'myVarA' => { 'UI' => { 'Step' => '.001', 'Max' => '0', 'Widget' => '"slider"', 'Min' => '1' }, 'value' => '0.5f', 'type' => 'float' }, 'myVarC' => { 'value' => '{1,0,0,1}', 'type' => 'float4' }, 'myVarB' => { 'value' => '1.0', 'type' => 'float' } };
You'd probably need to strip the comments around string initialisers and you don't specify how the initialiser {1,0,0,1} should be treated?
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Tricky Parsing Ko'an
by jmmistrot (Sexton) on Jan 25, 2007 at 06:26 UTC |