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.
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.