in reply to Print out hash
The warnings that were printed are telling you what's wrong:
Global symbol "$key" requires explicit package name (did you forget to + declare "my $key"?) at - line 11. Global symbol "$value" requires explicit package name (did you forget +to declare "my $value"?) at - line 11. Global symbol "$key" requires explicit package name (did you forget to + declare "my $key"?) at - line 12. Global symbol "$value" requires explicit package name (did you forget +to declare "my $value"?) at - line 12. Execution of - aborted due to compilation errors.
You need to declare those two variables, which is easy enough to do with while ( my ($key, $value) = each %count) {
showing the full code:
#! /usr/bin/perl use v5.12; use warnings; ## 2/15/19 my @people = qw{ fred barney fred wlima dino barney fred pebbles +}; my %count; # new empty hash $count{$_}++ foreach @people; while ( my ($key, $value) = each %count) { print "$key => $value\n"; } __END__ __OUTPUT__ wlima => 1 barney => 2 dino => 1 fred => 3 pebbles => 1
edit: the colon after the { looked like it was part of the code, started new paragraph instead.
|
---|