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.


In reply to Re: Dereferncing Hash Not Working by kennethk
in thread Dereferncing Hash Not Working by dirtdog

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.