Skip to content

Instantly share code, notes, and snippets.

@pkarman
Created May 14, 2018 20:32
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pkarman/7048fc398c46576cf52ab605aa9b4b94 to your computer and use it in GitHub Desktop.
Save pkarman/7048fc398c46576cf52ab605aa9b4b94 to your computer and use it in GitHub Desktop.
example parsing elecXML
#!/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