in reply to Syntax error - "my" within foreach loop
What's wrong? I found out, if you define %count outside the foreach loop with my %count; and omit "my" within the loop, it's working. But "my" within the loop results in the mentioned warning.Nothing! That's exactly how you do it
#!/usr/bin/perl use warnings; use strict; chomp(my @words = <STDIN>); my %count; foreach my $word (@words) { $count{$word} += 1; } foreach my $key (sort keys %count) { print "$key was found $count{$key} times\n"; }
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Syntax error - "my" within foreach loop
by Klammer (Acolyte) on Apr 13, 2008 at 19:30 UTC | |
by moritz (Cardinal) on Apr 13, 2008 at 19:33 UTC | |
by Klammer (Acolyte) on Apr 13, 2008 at 19:41 UTC | |
by moritz (Cardinal) on Apr 13, 2008 at 19:49 UTC | |
by halfcountplus (Hermit) on Apr 13, 2008 at 21:29 UTC | |
by Anonymous Monk on Apr 13, 2008 at 20:01 UTC |