Just as samtregar suggested, a two-pass approach seemed right to me too. First pass, parse(), parses the data into a hashref, and the second pass, get_command_args(), extracts the data from the hash ref to build the list of command arguments.

Two recursive subroutines in one perl program. I think I've been doing too much emacs lisp :-)

use strict; use warnings FATAL => 'all'; chomp (my @config = <DATA>); my $config = parse(); for my $server ( keys %$config ) { my @args = get_command_args( $config->{$server} ); print "enable $server $_\n" for @args; } sub get_command_args { my $cfg = shift; my @arg_list; for my $k ( keys %$cfg ) { my $v = $cfg->{$k}; my $args = "$k "; if ( ref $v ) { $args .= join " ", get_command_args( $v ); } else { $args .= "$v "; } push @arg_list, $args; } return @arg_list; } sub parse { my $hash; while ( my $line = shift @config ) { if ( $line =~ /(\S.*\S) \s* \{/x ) { # keywords + { my $k = $1; $hash->{$k} = parse(); } elsif ( $line =~ /(\S+) \s+ (\S+) \s* ; \s* $/x ) { # key valu +e; $hash->{$1} = $2; } elsif ( $line =~ /^ \s* } \s* $/x ) { # } return $hash; } else { die $line } # can't ha +ppen! } return $hash; } __DATA__ server { hostname test; console-type { type vt100; } account root { authentication-type { password "bubba"; } } }

Output:

enable server account root authentication-type password "bubba" enable server hostname test enable server console-type type vt100


In reply to Re: Approach to remove brackets from config by FunkyMonk
in thread Approach to remove brackets from config by ewhitt

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.