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

Re: Dereferncing Hash Not Working

by kennethk (Abbot)
on Jun 08, 2017 at 20:43 UTC ( #1192396=note: print w/replies, xml ) Need Help??


in reply to Dereferncing Hash Not Working

If you add strict and warnings, you'll get the error:
Argument "Attendance" isn't numeric in array or hash lookup at script. +pl line 36, <DATA> line 1. Argument "Sales" isn't numeric in array or hash lookup at script.pl li +ne 36, <DATA> line 1. Argument "Tickets" isn't numeric in array or hash lookup at script.pl +line 36, <DATA> line 1. Argument "Fans" isn't numeric in array or hash lookup at script.pl lin +e 36, <DATA> line 1.
When you do your hash access, $_ contains the entries of @report_name, not the indexes. Therefore $report_name[$_] is, for example, $report_name['Attendance'], which returns the 0 element, Attendance. So grab the value for Attendance repeatedly. You can fix this with
#!/usr/bin/env perl use strict; use warnings; use Data::Dumper; my @report_name = qw(Attendance Sales Tickets Fans); while (<DATA>) { my %by_report = split ' '; print Dumper(\%by_report); for (@report_name) { print "report_name is: $_\n"; if ( exists ($by_report{$_} ) ) { print "$_ Report is: $by_report{$_}\n"; } else { print "No data for $_ Report\n"; } } } __DATA__ Attendance 7424 Sales 7074 Tickets 350 Fans 109 Attendance 321541 Sales 321508 Tickets 31 Fans 101 Attendance 9208 Sales 9208 Fans 102 Attendance 9832 Sales 9829 Tickets 3 Fans 106 Attendance 1559058 Sales 1503174 Tickets 55404 Fans 104
Even better would be naming your loop variables (see Foreach Loops), to avoid confusion. You should also see Use strict warnings and diagnostics or die to understand why strict is your friend.

#11929 First ask yourself `How would I do this without a computer?' Then have the computer do it the same way.

Replies are listed 'Best First'.
Re^2: Dereferncing Hash Not Working
by dirtdog (Monk) on Jun 08, 2017 at 20:51 UTC

    Thank you Toolic and KennethK ! It's working now.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1192396]
help
Chatterbox?
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others having an uproarious good time at the Monastery: (2)
As of 2017-09-24 23:44 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    During the recent solar eclipse, I:









    Results (275 votes). Check out past polls.

    Notices?