From bd4c316c54b0771402d466676fbe8a703a2d74bc Mon Sep 17 00:00:00 2001 From: Nigel Horne Date: Mon, 25 Nov 2024 10:03:35 -0500 Subject: [PATCH] Refactor stepsabove() --- ged2site | 36 +++++++++++++++--------------------- 1 file changed, 15 insertions(+), 21 deletions(-) diff --git a/ged2site b/ged2site index dfd1cbd3..28bc4ff3 100755 --- a/ged2site +++ b/ged2site @@ -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, @@ -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; }