> On Nov 21, 2018, at 3:08 AM, Amanda Paziuk <acpaz...@gmail.com> wrote:
> 
> Folks,
> 
> I'm hoping someone can assist as I'm having difficulty with parsing a section 
> of the following configuration:
> 
> This is the code I have:
> 
>     open (IN, $file); # only sharing this because you need to know where 
> @list is derived.

using the three argument version of open with a lexically-scoped variable and 
adding error checking is preferred:

        open( my $in, ‘<‘, $file ) or die(“Can’t open file $file: $!”); 

>     while (<IN>) {
>         chomp;
>         next unless /file-id/;
>         my $datum = $_;
>         $datum =~ s/(^\s+|\s+$)//g;
>         $datum =~ s/file-id //g;
>         push @list, $datum; # should only contain '1', and '3'
>     }

That loop can be simplified:

        while( my $line = <$in> ) {
                if( $line =~ /^\s*file-id\s*(\d+)/ ) {
                        push( @list, $1 );
                }
        }

>     close IN;
> 
>     # ideally this would take a snippet of that config
>     $count = 0;
>     foreach my $i (@list){ # loops through dynamically-learned file IDs
>         open (IN, $file);
>         while (<IN>) {
>             chomp;
>             if (/^        file-id $i/) {
>                 $count = 1;
>             }
>             elsif (/^        exit/) { # this just keeps matching every exit 
> with that same indent
>                 $count = 0;
>             }
>             elsif ($count) {
>                 if (/text/) {
>                     push @logfiles, $_; # successfully captures only what I 
> want in the array
>                 }
>             }
>         }
>         close IN;
>     }
> 
> The first is to find all file-id lines then isolate for the number; the 
> second loop is to take a snip:
> 
>         file-id 1 
>             text
>         exit
> 
> What it winds up doing is:
> 
>         file-id 1 
>             text
>         exit
>         file-id 3
>             text
>         exit
>         exit
>         exit
>         exit
>         exit
>         exit 
>         … 

Is that what ends up in the @logfiles array? I don’t see how that can be. You 
are testing to see if text lines contain the string ’text’ before adding the 
line to the @logfiles array, and lines which only contain ‘exit’ do not contain 
’text'. Please show us your actual code.

> 
> This is the nightmare config structure that I need to deal with throughout 
> the file...lots of "exits" so it matches an awful lot...
> 
>     log 
>         file-id 1 
>             text
>         exit 
>         file-id 3
>             text
>         exit
>     exit 
> 
> The configuration is thousands of lines long, indented like that with exit at 
> various levels, so this is just one part of what I'm trying to analyze. What 
> it's doing is making the server work harder with all the extra stuff it's 
> catching and leaves me wide-open for false positives...
> 
> My question: does anyone have a better way to take those snippets, stopping 
> after the first match of "exit"? (Please don't flame, I'm asking for help.)
> 
> Amanda

--
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/


Reply via email to