Skip to content

Commit

Permalink
troubleshooting WIP: add _votedef2str() to PrefVote::Core::Input::CEF
Browse files Browse the repository at this point in the history
  • Loading branch information
ikluft committed Sep 6, 2023
1 parent 43265ca commit 4973dcb
Showing 1 changed file with 26 additions and 1 deletion.
27 changes: 26 additions & 1 deletion src/perl/prefvote/lib/PrefVote/Core/Input/CEF.pm
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,31 @@ sub debug_print
return;
}

# for testing/debugging: convert vote definition structure into a string
# recursive function to return a string for a vote definition structure or a portion within one
sub _votedef2str
{
my $vote_def = shift;

# if we got a scalar, treat it as a leaf node and return it
if ( not ref $vote_def ) {
return $vote_def;
}

# handle array
if ( ref $vote_def eq "ARRAY" ) {
return '[' . join(",", map( _votedef2str($_), @$vote_def)) . ']';
}

# handle hash
if ( ref $vote_def eq "HASH" ) {
return '{' . join(",", map($_ . "=>" . _votedef2str($vote_def->{$_}), sort keys %$vote_def)) . '}';
}

# otherwise stringify it
return "" . $vote_def;
}

#
# Condorcet Election Format (CEF) parser functions
#
Expand Down Expand Up @@ -301,7 +326,7 @@ sub parse
# parse candidate preference order from line
my @pref_order = $parser->parse( $line, $self->{vote_def} );
push @{$self->{ballots}}, \@pref_order;
$self->debug_print( "parse: pref_order=" . join( ",", @pref_order ) );
$self->debug_print( "parse: pref_order=" . _votedef2str( @pref_order ) );
}

# clean up
Expand Down

0 comments on commit 4973dcb

Please sign in to comment.