Forum Moderators: coopster & phranque

Message Too Old, No Replies

perl: stupid question but this drives me mad!

         

PsychoTekk

3:51 pm on Mar 9, 2002 (gmt 0)

10+ Year Member



hi, i want to read data out of a file into a hash,
the file looks like this:
key1 ~ 1
key2 ~ 5
key3 ~ 3
...

and that's the script:

open(db, "data.log");
while(<db>)
{
(my $data, my $count) = split(/ ~ /, $_);
my $table{$data} = chomp($count);
}
close(db);

the error occurs in the line where the hash gets the data, but there ain't no helpfull error message :(

i really wonder what i did wrong?
any help appreciated,
thanks in advance :)

amoore

3:57 pm on Mar 9, 2002 (gmt 0)

10+ Year Member



Two things you have done wrong:

  1. Not include the error message
  2. Your use of my inside the loop.

Try this:

my %table;
while(<db>) {
my ( $data, $count) = split(/ ~ /, $_);
$table{$data} = chomp($count);
}

hope it helps.

(edited by: amoore at 5:07 pm (utc) on Mar. 9, 2002)

PsychoTekk

4:01 pm on Mar 9, 2002 (gmt 0)

10+ Year Member



i tried it but i still get this error msg:

Software error:
Execution of logit.cgi aborted due to compilation errors.

i really have no clue what's wrong?

PsychoTekk

4:09 pm on Mar 9, 2002 (gmt 0)

10+ Year Member



when i don't set "use strict;" the script is executed but the hash is empty

amoore

5:07 pm on Mar 9, 2002 (gmt 0)

10+ Year Member



Are you usng "-w"? usually the error message is a little more informative than that. It at least tells you which line is the incorrect one.

Are you sure that' s the extent of the error?

Brett_Tabke

5:23 pm on Mar 9, 2002 (gmt 0)

WebmasterWorld Administrator 10+ Year Member Top Contributors Of The Month



You can't do this one:

my $table{$data} = chomp($count);

You can do
my %table;

But not an individual hash element.

Try this:
[perl]

open("db", "data.log");
while(<db>) {
(my $data, my $count) = split(/ \~ /);
$table{$data} = chomp($count);
}
close(db);
[/perl]

PsychoTekk

5:37 pm on Mar 9, 2002 (gmt 0)

10+ Year Member



thanks, i got a bit further now :)
data.log looks like this:
Mozilla/5.0 (Windows; U; Windows NT 5.0; en-US; rv:0.9.7) Gecko/20011221 ~ 1
Mozilla/4.0 (compatible; MSIE 5.0; Windows NT 5.0) Opera 5.01 [en] ~ 1
Mozilla/4.0 (compatible; MSIE 6.0; Windows 98; T312461; Q312461) ~ 1
Opera/5.01 (Windows NT 5.0; U) [en] ~ 1
Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1) ~ 1
Mozilla/4.0 (compatible; MSIE 5.0; Windows ME) Opera 6.01 [en] ~ 1
Mozilla/4.0 (compatible; MSIE 5.5; Windows 98; Win 9x 4.90) ~ 2
[.....]

this is the script so far:
#!perl

use strict;
use CGI::Carp qw(fatalsToBrowser);

print "Content-type: text/plain\n\n\n";

my $ua ="Opera/5.01 (Windows NT 5.0; U) [en]";

my %table;
open(db, "data.log");
while(<db>)
{
(my $data, my $count) = split(/ ~ /, $_);
$table{$data} = $count;
}
close(db);

print "UA:",$table{$ua},"\n";
print "EXST:",exists($table{$ua}),"\n";
print %table;

in the end i wanted to count how often the different browsers access the site.
however, the string is not found, although %table contains it...

the output looks like this:

UA:
EXST:
Mozilla/5.0 (Windows 98; U) Opera 5.12 [en]1
Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; Q312461)1
Mozilla/4.0 (compatible; MSIE 5.0; Windows NT 5.0) Opera 5.01 [en]1
Mozilla/4.0 (compatible; MSIE 6.0; Windows 98)1
Mozilla/4.0 (compatible; MSIE 5.01; Windows NT 5.0)1
[...]

why isn't the string found in the hash?

amoore

5:48 pm on Mar 9, 2002 (gmt 0)

10+ Year Member



This works for me:

#!/usr/local/bin/perl -w

use strict;
use Data::Dumper;

open(DB, 'data.log') or die "Can't open log: $!";
my %table;
while (<DB>) {
chomp;
my( $data, $count ) = split(/\s+~\s+/);
$table{ $data } = $count;
}
print Data::Dumper::Dumper( \%table );

PsychoTekk

10:46 am on Mar 10, 2002 (gmt 0)

10+ Year Member



thanks again, the script now works and is online :)

else
{my %table;
open(db, "useragent.log");
while(<db>)
{chomp;
(my $data, my $count) = split(/ ~ /, $_);
$table{$data} = $count;}
close(db);
$table{$ua}++;
my @entries = keys(%table);
@entries = sort(@entries);
open (db, "> useragent.log");
foreach(@entries)
{print (db "$_ ~ $table{$_}\n");}
close(db);}}