Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic
 
PerlMonks  

Dereferncing Hash Not Working

by dirtdog (Monk)
on Jun 08, 2017 at 20:28 UTC ( [id://1192393]=perlquestion: print w/replies, xml ) Need Help??

dirtdog has asked for the wisdom of the Perl Monks concerning the following question:

Hi Monks, I'd greatly appreciate any help on this piece of code. I'm expecting the script to display the value following each Report Type (Attendance Sales Tickets Fans). For some reason it keeps displaying the value from the Attendance Report only. The Data Dumper looks to have the correct key-> value pair so I can't figure out why looping and printing is not giving me the correct value for the key.

#!/usr/bin/env perl 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{$report_name[$_]} ) ) { print "$_ Report is: $by_report{$report_name[$_]}\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

Any help is much appreciated. Thank you.

Replies are listed 'Best First'.
Re: Dereferncing Hash Not Working
by toolic (Bishop) on Jun 08, 2017 at 20:39 UTC

    Tip #1 from the Basic debugging checklist: use warnings. You were trying to grab an array element by using a string value instead of a numeric value, which is what the warning message tells you.

    use warnings; use strict; 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
Re: Dereferncing Hash Not Working
by kennethk (Abbot) on Jun 08, 2017 at 20:43 UTC
    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.

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

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others avoiding work at the Monastery: (4)
As of 2024-03-28 21:07 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found