TStanley has asked for the wisdom of the Perl Monks concerning the following question:
It prints out the section headers, but each one of those is undef. Where exactly am I going wrong in the sub?#!/opt/perl5/bin/perl -w use strict; use Parse::RecDescent; use Data::Dumper; use vars qw($grammar); BEGIN{ $::RD_AUTOACTION = q{ [@item[1..$#item]] }; } $grammar = q( file: section(s) section: header pair(s?) header: /\[(\w+)\]/ { $1 } pair: /(\w+)\s?=\s?(\w+)(\s+[\#\;][\w\s]+)?\n/ { if(!defined $3){ [$1,$2]; }else{ [$1,$2,$3]; } } ); my $parser= Parse::RecDescent->new($grammar); my $text; { $/=undef; $text=<DATA>; } my $tree=$parser->file($text); my ($key,$x,%Config); my $config=deparse($tree); print Dumper($config); sub deparse{ my $aoa=shift; for $x (0..$#{$aoa}){ if(ref($aoa->[$x])){ $key=deparse($aoa->[$x]); }else{ if(scalar(@$aoa) == 2){ chomp $aoa->[0]; $y=$aoa->[0]; $Config{$y}=undef; return $key; }elsif(scalar(@$aoa) == 3){ if(defined $aoa->[2]){ $aoa->[2]=~s/^\s+[\;\#]//; chomp $aoa->[2]; $Config{$key}{$aoa->[0]}=[$aoa->[1],$aoa->[2]]; return; }else{ $Config{$key}{$aoa->[0]}=$aoa->[1]; return; } } } } my $return=\%Config; return $return; } __DATA__ [Section1] key1=value1 key2=value2 #Comment2 key3=value3 [Section2] key4=value4 key5=value5 key6=value6 ;Comment 6
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: How do I set up the recursion?
by sauoq (Abbot) on May 29, 2003 at 20:07 UTC | |
|
Re: How do I set up the recursion?
by pzbagel (Chaplain) on May 29, 2003 at 20:09 UTC | |
by sauoq (Abbot) on May 29, 2003 at 20:21 UTC | |
by pzbagel (Chaplain) on May 29, 2003 at 21:18 UTC | |
by TStanley (Canon) on May 29, 2003 at 23:57 UTC | |
|
Re: How do I set up the recursion?
by BrowserUk (Patriarch) on May 30, 2003 at 19:53 UTC | |
|
Re: How do I set up the recursion?
by Fletch (Bishop) on May 29, 2003 at 18:58 UTC |