in reply to nesting loops help?
and blindly iterating over $search_files. Idk I think I also found an issue with your next and question your use of that inner while altogether.#!/usr/bin/perl #^^^ added / after ! use warnings; open my $wave, '>', 'Wave' or die "Can't open $wave: $!"; open my $keywords, '<', 'Agents' or die "Can't open keywords: $!"; open my $search_file, '<', 'Definitions' or die "Can't open search f +ile: $!"; my $keyword_or = join '|', map {chomp;qr/\Q$_\E/} <$keywords>; close $keywords; my $regex = qr|\b($keyword_or)\b|; OUTER: while (my $line = <$search_file>) { # ^^^^^^^ using just $_ is for chodes INNER: while ($line =~ m/$regex/g) #<~ why is this a "while" an not an "i +f"? { # skip if... if ( $line =~ m/(?:SCRIPTNAME|DESCRIPTION)/ ) { #^ if you're not capturing $1, don't keep it i +n memory next INNER; # ^^^^^ labels when using "nested" loops can be very help +ful } print $wave $line; } } close $wave; close $search_file; <code> Final note, you're opening yourself up to some adversarial input attac +ks by trusting <code>$keyword_or
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: nesting loops help?
by Anonymous Monk on Mar 11, 2022 at 19:42 UTC |