Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

Re: Print out hash

by pryrt (Abbot)
on Feb 15, 2019 at 16:15 UTC ( [id://1229965]=note: print w/replies, xml ) Need Help??


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.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1229965]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others goofing around in the Monastery: (3)
As of 2024-04-24 02:49 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found