Created
May 14, 2018 20:32
-
-
Save pkarman/7048fc398c46576cf52ab605aa9b4b94 to your computer and use it in GitHub Desktop.
example parsing elecXML
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env perl | |
use strict; | |
use warnings; | |
use XML::Simple qw( :strict ); | |
use File::Slurper 'read_text'; | |
use Data::Dump qw( dump ); | |
my $usage = "$0 file.xml\n"; | |
my $file = shift(@ARGV) or die $usage; | |
#my $xml = read_file($file); | |
my $results = XMLin( $file, ForceArray => [], KeyAttr => {} ); | |
#dump( [ keys %$results ] ); | |
#dump( [ keys %{ $results->{County}->{Election} } ] ); | |
my $precincts | |
= $results->{County}->{Election}->{ElectionTally}->[0]->{PrecinctTally}; | |
for my $precinct (@$precincts) { | |
printf( "%s [%s]\n", $precinct->{precinct}, $precinct->{precinctName} ); | |
for my $tally ( @{ $precinct->{SplitTally}->[0]->{ContestTally} } ) { | |
next unless $tally->{CastVoteTally}; | |
printf( " %s\n", $tally->{contestName} ); | |
#dump $tally; | |
my $votes | |
= ref( $tally->{CastVoteTally} ) eq 'ARRAY' | |
? $tally->{CastVoteTally} | |
: [ $tally->{CastVoteTally} ]; | |
for my $vote (@$votes) { | |
printf( " %s => %s\n", $vote->{choice}, $vote->{count} ); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment