Skip to content

Commit

Permalink
Refactor stepsabove()
Browse files Browse the repository at this point in the history
  • Loading branch information
nigelhorne committed Nov 25, 2024
1 parent fd59ccd commit bd4c316
Showing 1 changed file with 15 additions and 21 deletions.
36 changes: 15 additions & 21 deletions ged2site
Original file line number Diff line number Diff line change
Expand Up @@ -10273,7 +10273,7 @@ sub all_records_have_date
# Check if the date is valid and matches required format
if((!defined $date) || ($date !~ /^\d/) || ($date =~ /[a-z]$/i) ||
($date =~ /[\/\-]/) || ($date =~ / to /) || !date_parser_cached(date => $date)) {
if(defined($date)) {
if((defined($date)) && ($date !~ / to /) && ($date !~ /[\/\-]/)) {
# Log a warning if date is invalid
complain({
person => $person,
Expand Down Expand Up @@ -11373,36 +11373,30 @@ sub Gedcom::Individual::relationship_down

sub stepsabove
{
my $person = shift;
my $target = shift;
my $count = shift;

return -1 if($count == -1);
my ($person, $target, $count) = @_;
$count ||= 0; # Ensure count has a default value if not provided.

if(!defined($target)) {
# Validate inputs
if((!defined $person) || !defined $target) {
print STDERR "\n";
my $i = 0;
while((my @call_details = caller($i++))) {
print STDERR "\t", colored($call_details[2] . ' of ' . $call_details[1], 'red'), "\n";
while (my @call_details = caller($i++)) {
print STDERR "\t", colored("$call_details[2] of $call_details[1]", 'red'), "\n";
}
die 'Usage: stepsabove($person, $target, $count)';
}

if($person->xref() eq $target->xref()) {
return $count;
}

my @father = $person->father();
if(my $father = $father[0]) {
my $rc = stepsabove($father, $target, $count + 1);
return $rc if($rc != -1);
}
# Found the target
return $count if $person->xref() eq $target->xref();

my @mother = $person->mother();
if(my $mother = $mother[0]) {
return stepsabove($mother, $target, $count + 1);
# Recursive calls for father and mother
for my $parent($person->father(), $person->mother()) {
next unless defined $parent; # Skip undefined parents
my $rc = stepsabove($parent, $target, $count + 1);
return $rc if $rc != -1; # Return result if found
}

# Target not found
return -1;
}

Expand Down

0 comments on commit bd4c316

Please sign in to comment.