This is a greatly simplified script (that doesn't do anything here) used to report webpage hits.
The honorable Monks stress the use of strict but I am confused about what to do in a situation
where variables are created on the fly (as in the first 'for' loop) and then used in the second
'for' loop. It all works fine if I don't use strict. The page names to be tracked are not known
until the data file is read.
#!/usr/bin/perl
use strict;
my $datafile = "data.txt";
my @now = localtime(time);
open(FIL,"<$datafile") or die "Can't open data file: $!\n";
my @data = <FIL>;
close(FIL);
my %maxCount;
foreach my $line (@data) {
(my $timestamp, my $page, my $moredata) = split('\|',$line);
my @hits = localtime($timestamp);
# Here I create and preset an array with a variable name defined b
+y $page
@$page = (0,0,0,0) if ($$page[3] eq '');
if ($hits[7] == $now[7]) { $$page[0]++; }
if ($hits[4] == $now[4]) { $$page[1]++; }
if ($hits[5] == $now[5]) { $$page[2]++; }
$maxCount{'$page'}++;
}
my @pages = keys %maxCount; # Gets list of page names
my $dAry = ""; my $wAry = ""; my $mAry = "";
foreach my $page (@pages) {
$dAry .= "$$page[0],";
$wAry .= "$$page[1],";
$mAry .= "$$page[2],";
}
#... continued
exit;
Is it possible to have for a hash array element contain a list array? (a possible solution)
Thanks in advance....
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: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.