in reply to Nested foreach

@ignore should stay intact because you're not modifying it. There's a couple of other problems with the code though.
- You're reusing $line
- You're using $_ although it never gets initialized

I think this is what you intended to do (untested)
$_ = qr/\Q$_\E/ for @ignore; # precompile regexes foreach my $line (@words){ $line =~ tr/A-Z/a-z/; # or lc() foreach my $ignore (@ignore){ $line =~ s/$ignore//g; } }
--perlplexer

Replies are listed 'Best First'.
Re: Re: Nested foreach
by esoteric neonate (Novice) on Apr 25, 2003 at 17:33 UTC
    I'm still getting the same problem after I begun using a different variable instead of $line, why does it still refuse to ignore my words?
    my @words; my %search; my $first; my $second; my $line; my @ignore = qq(a and the this i me us our ok abc); my $file = "abc.txt"; my $count = "0"; open (FILE, $file) or die "Error $!"; @words = <FILE>; chomp(@words); close FILE; my @search= @words; foreach my $line (@words) { $line =~ tr/A-Z/a-z/; foreach my $ignore (@ignore) { $ignore =~ s/\b$ignore\b//; } } foreach my $line (@words) { # splitting words on a white space but allowing contractions and hyphe +ns while ($line =~ /([[:alpha:]]+(?:'[[:alpha:]]+)?)/g) { if (exists ($search{$1})) { $search{$1}++; } else { $search{$1}=""; $search{$1}++; } } } while (($first,$second)=each(%search)) { print "$first -- $second\n"; }
        foreach my $line (@words) { $line =~ tr/A-Z/a-z/; foreach my $ignore (@ignore) { $line =~ s/\b$ignore\b//g; } }
        doesn't change the outcome of the program either :( No words are ignored but you were right, I need the //g :)