Problem splitting lines in a file

Discussion in 'Perl' started by Mothra, Jan 12, 2005.

  1. Mothra

    Mothra Guest

    I'm trying to parse a log file using ';' as a newline and then wherever
    I find items inside '(..)', indenting those lines thus:


    foo;bar(foo;bar(foobar);foo)foobar

    becomes

    foo
    bar(
    foo
    bar(
    foobar
    )
    foo
    )
    foobar

    What I've got so far is below - the problem is that when there are
    nested sets of parentheses, I can't increase the indents accordingly.
    Also, I can't work out how to get the closing ')' on a line by itself.

    I'm in a bit of a pickle - can anyone help?

    What I've got so far follows:


    #!/usr/bin/perl -w
    use strict;
    $|=0;

    my @in = <>;
    my $tbchr = "\t";
    my $tbcnt = 0;

    for(@in){
    my @ln=split(';', $_);
    my $i;
    for ($i=0; $i<@ln; $i++){
    chomp($ln[$i]);
    my @subln;
    if ( $ln[$i] =~ /\([^)]/ ) {
    @subln = split('\(',$ln[$i]);
    my $k;for($k=0;$k<@subln;$k++){ $subln[$k] .=
    '(' }
    } elsif ( $ln[$i] =~ /[^(]\)/ ) {
    @subln = split('\)',$ln[$i]);
    my $k;for($k=0;$k<@subln;$k++){ $subln[$k] .=
    ')' }
    } else {
    @subln = $ln[$i];
    }


    for(@subln){
    my $j;
    for($j=0; $j<$tbcnt; $j++){ print $tbchr }
    print "$_\n";
    $tbcnt++ if /\(/;
    $tbcnt-- if /\)/;
    }

    }
    }
    Mothra, Jan 12, 2005
    #1
    1. Advertising

  2. Mothra

    vkeyboard Guest

    I'd probably use a counter in a hash to keep track of the ()'s. One key
    for the ) and one key for the (. To get the ) on a line by itself I'd do a
    =~ s/)/\n)/

    Can't remember if you'll need to escape that ) or not.

    Mothra wrote:

    > I'm trying to parse a log file using ';' as a newline and then wherever
    > I find items inside '(..)', indenting those lines thus:



    > foo;bar(foo;bar(foobar);foo)foobar


    > becomes


    > foo
    > bar(
    > foo
    > bar(
    > foobar
    > )
    > foo
    > )
    > foobar


    > What I've got so far is below - the problem is that when there are
    > nested sets of parentheses, I can't increase the indents accordingly.
    > Also, I can't work out how to get the closing ')' on a line by itself.


    > I'm in a bit of a pickle - can anyone help?


    > What I've got so far follows:



    > #!/usr/bin/perl -w
    > use strict;
    > $|=0;


    > my @in = <>;
    > my $tbchr = "t";
    > my $tbcnt = 0;


    > for(@in){
    > my @ln=split(';', $_);
    > my $i;
    > for ($i=0; $i<@ln; $i++){
    > chomp($ln[$i]);
    > my @subln;
    > if ( $ln[$i] =~ /([^)]/ ) {
    > @subln = split('(',$ln[$i]);
    > my $k;for($k=0;$k<@subln;$k++){ $subln[$k] .=
    > '(' }
    > } elsif ( $ln[$i] =~ /[^(])/ ) {
    > @subln = split(')',$ln[$i]);
    > my $k;for($k=0;$k<@subln;$k++){ $subln[$k] .=
    > ')' }
    > } else {
    > @subln = $ln[$i];
    > }



    > for(@subln){
    > my $j;
    > for($j=0; $j<$tbcnt; $j++){ print $tbchr }
    > print "$_n";
    > $tbcnt++ if /(/;
    > $tbcnt-- if /)/;
    > }


    > }
    > }
    vkeyboard, Jan 12, 2005
    #2
    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.

Share This Page