Parsing of blocks (e.g. foo { bar })

Discussion in 'Perl Misc' started by Tor Houghton, Oct 30, 2003.

  1. Tor Houghton

    Tor Houghton Guest

    Hi,

    I'm wondering if there is a more elegant way of parsing "blocks" of data.

    I have a configuration file, thus:

    # comment

    some.host.name {
    ; another comment
    /etc/hostname.fxp0
    /etc/hosts
    /etc/mail
    }

    And I currently parse this using the following code:

    while(<CONF>) {
    next if(/^\s*(\#|;)|^\s*$/);
    if(m#(\S+)\s+{# .. m#}#) {
    $HOST=$1 if $1;
    if(!/\{|\}/) {
    print "add: $HOST ",$_;
    }
    }
    }

    Before I go "all out" and complete this section of the code using the
    above technique, I was wondering if there was other (better) ways of doing
    this (for one, I'm not sure how to catch out any errors in the file, for
    instance:

    some.host.here {
    another.host {
    /usr
    /opt
    }
    /etc
    /var/mail
    }

    ... here the current code fails miserably. Mind you, I'm probably the only
    one going to use this, so that may not be such a big deal :-> )

    Thanks!

    Tor
    Tor Houghton, Oct 30, 2003
    #1
    1. Advertising

  2. -----BEGIN PGP SIGNED MESSAGE-----
    Hash: SHA1

    On Thu, 30 Oct 2003 12:36:39 +0000, Tor Houghton wrote:

    > Hi,
    >
    > I'm wondering if there is a more elegant way of parsing "blocks" of data.

    [snip]
    > Before I go "all out" and complete this section of the code using the
    > above technique, I was wondering if there was other (better) ways of doing
    > this (for one, I'm not sure how to catch out any errors in the file, for
    > instance:
    >
    > some.host.here {
    > another.host {
    > /usr
    > /opt
    > }
    > /etc
    > /var/mail
    > }
    >
    > .. here the current code fails miserably. Mind you, I'm probably the only
    > one going to use this, so that may not be such a big deal :-> )


    You didn't really say how you wanted to handle this condition. Is it a
    valid condition, or can your program just abort?

    If you need to handle that condition, then instead of using the scalar
    ".."'s flag, use a nesting counter:
    my $nest = 0;
    while(<CONF>) {
    $nest++ if (/(\S+)\s*\{/);
    $nest-- if (/\}/);
    if($nest == 1) { # Or, $nest >= 1
    # Handle conditions.
    }
    }

    Actually, thinking about it, aborting would use very similar logic.

    - Brian
    -----BEGIN PGP SIGNATURE-----
    Version: GnuPG v1.2.2 (GNU/Linux)

    iD8DBQE/oStgiK/rA3tCpFYRAjpzAKCDOI/WgKFUnNmnG7Bf4hNuTSg1qQCgxvWm
    vrQkYrxLEaFNq65GM8MwIEI=
    =3Yc1
    -----END PGP SIGNATURE-----
    Brian Harnish, Oct 30, 2003
    #2
    1. Advertising

  3. Tor Houghton

    Tor Houghton Guest

    On Thu, 30 Oct 2003 15:16:30 +0000, Brian Harnish wrote:

    > [snip]
    >
    > You didn't really say how you wanted to handle this condition. Is it a
    > valid condition, or can your program just abort?
    >


    Hm. No, it's an invalid condition, so the program should just abort.

    > If you need to handle that condition, then instead of using the scalar
    > ".."'s flag, use a nesting counter:
    > my $nest = 0;
    > while(<CONF>) {
    > $nest++ if (/(\S+)\s*\{/);
    > $nest-- if (/\}/);
    > if($nest == 1) { # Or, $nest >= 1
    > # Handle conditions.
    > }
    > }
    >
    > Actually, thinking about it, aborting would use very similar logic.
    >


    Ah. I didn't think about doing it that way. Thanks!

    Tor
    Tor Houghton, Oct 30, 2003
    #3
  4. Tor Houghton <> wrote:

    > I'm wondering if there is a more elegant way of parsing "blocks"
    > of data.


    [snip]

    > Before I go "all out" and complete this section of the code using
    > the above technique, I was wondering if there was other (better)
    > ways of doing this


    If you're not tied to that particular format you could use
    Config::Simple or one of the numerous other Config::* modules in the
    CPAN.

    But I may be revealing some ignorance here; your data may be the
    output of another program that requires that format.

    --
    David Wall
    David K. Wall, Oct 30, 2003
    #4
  5. Tor Houghton

    Tor Houghton Guest

    On Thu, 30 Oct 2003 18:55:42 +0000, David K. Wall wrote:

    > If you're not tied to that particular format you could use
    > Config::Simple or one of the numerous other Config::* modules in the
    > CPAN.
    >
    >


    Thanks for the tip!

    For this application, however, I need to make sure it'll work on a system
    "out of the box", without the worry of whether a certain CPAN module needs
    to be installed.

    Tor
    Tor Houghton, Oct 30, 2003
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Jonathan Bartlett
    Replies:
    7
    Views:
    451
    Peter Nilsson
    Jul 8, 2005
  2. Wejn
    Replies:
    2
    Views:
    117
    Michal
    Nov 29, 2003
  3. matt
    Replies:
    1
    Views:
    255
    George Ogata
    Aug 6, 2004
  4. Max Williams
    Replies:
    10
    Views:
    200
    Max Williams
    Dec 15, 2007
  5. Gunnar Hjalmarsson
    Replies:
    12
    Views:
    306
    Darren Dunham
    Feb 24, 2005
Loading...

Share This Page