Re: Code Assistance Pls
On 2018-11-21 3:08 a.m., Amanda Paziuk wrote: 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: push @list, $datum; # should only contain '1', and '3' > > ... > foreach my $i (@list){ # loops through dynamically-learned file IDs open (IN, $file); while () { chomp; if (/^file-id $i/) { If $i contains 1 then this will match "file-id 10" or "file-id 1234" or any number whose first digit is 1. You need to either use anchors: if ( /^file-id \b$i\b/ ) { Or capture the number and do numerical comparison: if ( /^file-id (\d+)/ && $1 == $i ) { John -- To unsubscribe, e-mail: beginners-unsubscr...@perl.org For additional commands, e-mail: beginners-h...@perl.org http://learn.perl.org/
Re: Code Assistance Pls
> On Nov 21, 2018, at 3:08 AM, Amanda Paziuk 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 () { > 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 () { > 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/
Re: Code Assistance Pls
These 2 lines: if (/^ file-id $i/) { and elsif (/^ exit/) { Need to be like this: if (/^\s+file-id $/i) { # 2 CHANGES HERE - PAY CLOSE ATTENTION and elsif (/^\s+exit/i) { I am not sure this will fix all your problems. Mike On 11/21/2018 5:08 AM, Amanda Paziuk 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. while () { 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' } 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 () { 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 / ... 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
Code Assistance Pls
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. while () { 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' } 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 () { 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 * ... 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