in reply to nesting loops help?

In addition to the suggestion to use strict;, I recommend you not rely on implicit variables.
#!/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
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.

Replies are listed 'Best First'.
Re^2: nesting loops help?
by Anonymous Monk on Mar 11, 2022 at 19:42 UTC
    In addition to the suggestion to use strict;

    you didnt do that and your code doesnt work with strict

    why is this a "while" an not an "if"?

    read the fine manual

    using just $_ is for chodes

    big man