CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Intermediate: Re: [stuckinarut] HASH-O-RAMA Data Processing Problem: Edit Log



Zhris
Enthusiast

Feb 25, 2015, 11:36 AM


Views: 31986
Re: [stuckinarut] HASH-O-RAMA Data Processing Problem

Hi,

I've had a little more time to work on this. I have adjusted the script to make it a little more readable and have roughly generated the desired score and error outputs ( strings for now ). I haven't "fixed" anything I invisage being incorrect, instead am awaiting the feedback from your test drive ;):


Code
use strict; 
use warnings;
use Data::Dumper;

local $, = "\t";
local $\ = "\n";

#####

# fh begin.

my $output_scores_str = '';
my $output_errors_str = '';

my $input_fh = \*DATA; # 'listQ.txt'
open my $output_scores_fh, '>', \$output_scores_str; # 'logscores.csv'
open my $output_errors_fh, '>', \$output_errors_str; # 'logerrors.csv'

#####

# init.

my $case_sensitive = 0;

my $band_lookup =
{
3 => '80M',
7 => '40M',
};

my $contestants = { };

my $potential_non_submitters = { };

#####

# first sweep ( load input data into hash ).

while ( my $line = <$input_fh> )
{
$line =~ s/\s+$//;

my ( $freq, $logcall, $logname, $logmult, $callsign, $callname, $callmult ) =
map { uc $_ unless $case_sensitive }
(split( ' ', $line ))[1,5..10];

# lookup band via frequency.
my $band = $band_lookup->{substr( $freq, 0, 1 )}; # todo: // error.

# add a new contestant to contestants if we haven't seen them before.
unless ( defined $contestants->{$logcall} )
{
$contestants->{$logcall} =
{
logname => $logname,
logmult => $logmult,
};
}

# add this entry to the contestants entries.
push @{$contestants->{$logcall}->{bands}->{$band}},
{
callsign => $callsign,
callname => $callname,
callmult => $callmult,
};
}

#####

# second sweep ( process hash, generate logs ).

print $output_scores_fh 'LOGCALL', 'QSOS', 'MULT', 'NAME';
print $output_errors_fh 'LOGCALL', 'CALLSIGN', 'ERRORTYPES';

while ( my ( $logcall, $contestant ) = each ( %$contestants ) )
{
my $verified = 0;

while ( my ( $band, $entries ) = each ( %{$contestant->{bands}} ) )
{
for my $entry ( @$entries )
{
# mark as seen. Used when checking for duplicate entries.
$entry->{seen} = 1;

# validate entry.
if ( not defined $contestants->{$entry->{callsign}} ) # invalid callsign.
{
$entry->{errors}->{'NIL(BANDQSO)'} = 1;

$potential_non_submitters->{$entry->{callsign}}++;
}
elsif ( ( grep { $_->{seen} and $_->{callsign} eq $entry->{callsign} } @$entries ) > 1 ) # duplicate entry.
{
$entry->{errors}->{'DUPE(BANDQSO)'} = 1;
}
elsif ( $entry->{callname} ne $contestants->{$entry->{callsign}}->{logname} ) # invalid callname.
{
$entry->{errors}->{'INVALID(NAME)'} = 1;
}
elsif ( $entry->{callmult} ne $contestants->{$entry->{callsign}}->{logmult} ) # invalid callmult.
{
$entry->{errors}->{'INVALID(MULT)'} = 1;
}
elsif ( not grep { $_->{callsign} eq $logcall } @{$contestants->{$entry->{callsign}}->{bands}->{$band}} ) # no return entry.
{
$entry->{errors}->{'NIL(CALLSIGN)'} = 1;
}

# log errors if any, or increment verified count.
if ( keys %{$entry->{errors}} )
{
print $output_errors_fh $logcall, $entry->{callsign}, keys %{$entry->{errors}}; # todo: errors better as list not hash.
}
else
{
$verified++;
}
}
}

# log score.
print $output_scores_fh $logcall, $verified, $contestant->{logmult}, $contestant->{logname};
}

#####

# dump.

{
local $, = "\n";
print Dumper $contestants, $potential_non_submitters;
}

#####

# fh end.

close $input_fh;
close $output_scores_fh;
close $output_errors_fh;

print $output_scores_str;
print $output_errors_str;

#####

__DATA__
QSO: 7040 CW 2015-01-22 0200 W7WHY Tom OR N6ZFO BILL CA
QSO: 7040 CW 2015-01-22 0200 W7WHY Tom OR N6ZFO BILL CA
QSO: 7040 CW 2015-01-22 0200 W7WHY Tom OR N6ZFO BILL CA
QSO: 7040 CW 2015-01-22 0201 W7WHY Tom OR W9RE MIKE IN
QSO: 3542 CW 2015-01-22 0231 W7WHY Tom OR N6ZF BILL CA
QSO: 3540 CW 2015-01-22 0232 W7WHY Tom OR W6NV OLI CA
QSO: 3542 CW 2015-01-22 0246 W7WHY Tom OR W9RE MIKE IN
QSO: 7000 CW 2015-01-22 0201 W9RE MIKE IN W7WHY TOM Or
QSO: 7000 CW 2015-01-22 0221 W9RE MIKE IN N6ZFO BILL Ca
QSO: 3500 CW 2015-01-22 0231 W9RE MIKE IN N6ZFO BIL Ca
QSO: 3500 CW 2015-01-22 0246 W9RE MIKE IN W7WHY TOM Or
QSO: 3500 CW 2015-01-22 0249 W9RE MIKE IN W6NV OLI Ca
QSO: 7040 CW 2015-01-22 0201 N6ZFO BILL CA W7WHY TOM OR
QSO: 7040 CW 2015-01-22 0221 N6ZFO BILL CA W9RR MIKE IN
QSO: 7042 CW 2015-01-22 0222 N6ZFO BILL CA N2NL DAVE FL
QSO: 3543 CW 2015-01-22 0231 N6ZFO BILL CA W9RE MIKE IN
QSO: 3542 CW 2015-01-22 0231 N6ZFO BILL CA W7WHY TOM OR
QSO: 3544 CW 2015-01-22 0235 N6ZFO BILL CA W6NV OLI CA


Output:

Code
$VAR1 = { 
'N6ZFO' => {
'logname' => 'BILL',
'logmult' => 'CA',
'bands' => {
'80M' => [
{
'seen' => 1,
'callmult' => 'IN',
'errors' => {},
'callsign' => 'W9RE',
'callname' => 'MIKE'
},
{
'seen' => 1,
'callmult' => 'OR',
'errors' => {
'NIL(CALLSIGN)' => 1
},
'callsign' => 'W7WHY',
'callname' => 'TOM'
},
{
'seen' => 1,
'callmult' => 'CA',
'errors' => {
'NIL(BANDQSO)' => 1
},
'callsign' => 'W6NV',
'callname' => 'OLI'
}
],
'40M' => [
{
'seen' => 1,
'callmult' => 'OR',
'errors' => {},
'callsign' => 'W7WHY',
'callname' => 'TOM'
},
{
'seen' => 1,
'callmult' => 'IN',
'errors' => {
'NIL(BANDQSO)' => 1
},
'callsign' => 'W9RR',
'callname' => 'MIKE'
},
{
'seen' => 1,
'callmult' => 'FL',
'errors' => {
'NIL(BANDQSO)' => 1
},
'callsign' => 'N2NL',
'callname' => 'DAVE'
}
]
}
},
'W7WHY' => {
'logname' => 'TOM',
'logmult' => 'OR',
'bands' => {
'80M' => [
{
'seen' => 1,
'callmult' => 'CA',
'errors' => {
'NIL(BANDQSO)' => 1
},
'callsign' => 'N6ZF',
'callname' => 'BILL'
},
{
'seen' => 1,
'callmult' => 'CA',
'errors' => {
'NIL(BANDQSO)' => 1
},
'callsign' => 'W6NV',
'callname' => 'OLI'
},
{
'seen' => 1,
'callmult' => 'IN',
'errors' => {},
'callsign' => 'W9RE',
'callname' => 'MIKE'
}
],
'40M' => [
{
'seen' => 1,
'callmult' => 'CA',
'errors' => {},
'callsign' => 'N6ZFO',
'callname' => 'BILL'
},
{
'seen' => 1,
'callmult' => 'CA',
'errors' => {
'DUPE(BANDQSO)' => 1
},
'callsign' => 'N6ZFO',
'callname' => 'BILL'
},
{
'seen' => 1,
'callmult' => 'CA',
'errors' => {
'DUPE(BANDQSO)' => 1
},
'callsign' => 'N6ZFO',
'callname' => 'BILL'
},
{
'seen' => 1,
'callmult' => 'IN',
'errors' => {},
'callsign' => 'W9RE',
'callname' => 'MIKE'
}
]
}
},
'W9RE' => {
'logname' => 'MIKE',
'logmult' => 'IN',
'bands' => {
'80M' => [
{
'seen' => 1,
'callmult' => 'CA',
'errors' => {
'INVALID(NAME)' => 1
},
'callsign' => 'N6ZFO',
'callname' => 'BIL'
},
{
'seen' => 1,
'callmult' => 'OR',
'errors' => {},
'callsign' => 'W7WHY',
'callname' => 'TOM'
},
{
'seen' => 1,
'callmult' => 'CA',
'errors' => {
'NIL(BANDQSO)' => 1
},
'callsign' => 'W6NV',
'callname' => 'OLI'
}
],
'40M' => [
{
'seen' => 1,
'callmult' => 'OR',
'errors' => {},
'callsign' => 'W7WHY',
'callname' => 'TOM'
},
{
'seen' => 1,
'callmult' => 'CA',
'errors' => {
'NIL(CALLSIGN)' => 1
},
'callsign' => 'N6ZFO',
'callname' => 'BILL'
}
]
}
}
};

$VAR2 = {
'N6ZF' => 1,
'W9RR' => 1,
'N2NL' => 1,
'W6NV' => 3
};

LOGCALL QSOS MULT NAME
N6ZFO 2 CA BILL
W7WHY 3 OR TOM
W9RE 2 IN MIKE

LOGCALL CALLSIGN ERRORTYPES
N6ZFO W7WHY NIL(CALLSIGN)
N6ZFO W6NV NIL(BANDQSO)
N6ZFO W9RR NIL(BANDQSO)
N6ZFO N2NL NIL(BANDQSO)
W7WHY N6ZF NIL(BANDQSO)
W7WHY W6NV NIL(BANDQSO)
W7WHY N6ZFO DUPE(BANDQSO)
W7WHY N6ZFO DUPE(BANDQSO)
W9RE N6ZFO INVALID(NAME)
W9RE W6NV NIL(BANDQSO)
W9RE N6ZFO NIL(CALLSIGN)


Regards,

Chris


(This post was edited by Zhris on Feb 25, 2015, 11:58 AM)


Edit Log:
Post edited by Zhris (Enthusiast) on Feb 25, 2015, 11:39 AM
Post edited by Zhris (Enthusiast) on Feb 25, 2015, 11:42 AM
Post edited by Zhris (Enthusiast) on Feb 25, 2015, 11:44 AM
Post edited by Zhris (Enthusiast) on Feb 25, 2015, 11:45 AM
Post edited by Zhris (Enthusiast) on Feb 25, 2015, 11:50 AM
Post edited by Zhris (Enthusiast) on Feb 25, 2015, 11:57 AM
Post edited by Zhris (Enthusiast) on Feb 25, 2015, 11:58 AM


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives