use GA class

master
Vincent Schweiger 2024-01-28 16:40:31 +01:00
parent 9a95a8add6
commit 12f0643a83
Signed by: vincent
GPG Key ID: F83B0F0E383CC23F
1 changed files with 60 additions and 28 deletions

View File

@ -1,12 +1,57 @@
#!/usr/bin/perl
package GA;
use strict; use warnings; use Carp;
sub new {
my $class = shift;
my $self = { @_ };
croak "bad GA arguments" unless defined $self->{top} and defined $self->{middle} and defined $self->{bottom};
return bless $self, $class;
}
sub fromstr {
my ($class, $str) = @_;
croak "no string provided" unless defined $str;
$str =~ /([0-9]+)\/([0-9]+)\/([0-9]+)/;
my $top = $1;
my $middle = $2;
my $bottom = $3;
return new($class, top => $top, middle => $middle, bottom => $bottom);
}
sub str {
my $self = shift;
return "$self->{top}/$self->{middle}/$self->{bottom}";
}
sub comp{
my ($class, $a, $b) = @_;
if ($a->{top} < $b->{top}) {
return -1;
} elsif ($a->{top} == $b->{top}) {
if ($a->{middle} < $b->{middle}) {
return -1;
}
elsif($a->{middle} == $b->{middle}) {
return $a->{bottom} <=> $b->{bottom};
}
}
return 1;
}
package main;
use strict; use warnings;
use Getopt::Long;
my $newlines = '';
my $file = '';
my $ranges = '';
GetOptions ("newlines" => \$newlines, # print newlines
"file=s" => \$file # input file
"file=s" => \$file, # input file
"ranges" => \$ranges # print with ranges
) or die("Error in command line arguments\n");
my %seen = ();
@ -36,37 +81,24 @@ if ($file) {
}
}
my @uniq = sort gw_comp keys %seen;
my @uniq;
foreach my $item (keys %seen) {
push @uniq, GA->fromstr($item);
}
@uniq = sort { GA->comp($a, $b) } @uniq;
# my @uniq = sort gw_comp keys %seen;
if (@uniq) {
if ($newlines) {
foreach my $item (@uniq) {
print "$item\n";
print $item->str, "\n";
}
} else {
print "@uniq\n";
foreach my $item (@uniq) {
print $item->str, " ";
}
print "\n";
}
}
sub gw_comp{
# extract $1/$2/$3 for both GAs
$a =~ /([0-9]+)\/([0-9]+)\/([0-9]+)/;
my $a1 = $1;
my $a2 = $2;
my $a3 = $3;
$b =~ /([0-9]+)\/([0-9]+)\/([0-9]+)/;
my $b1 = $1;
my $b2 = $2;
my $b3 = $3;
if ($a1 < $b1) {
return -1;
} elsif ($a1 == $b1) {
if ($a2 < $b2) {
return -1;
}
elsif($a2 == $b2) {
return $a3 <=> $b3;
}
}
return 1;
}