From 12f0643a83410fd125071a34cfc211f4912acb9f Mon Sep 17 00:00:00 2001 From: Vincent Schweiger Date: Sun, 28 Jan 2024 16:40:31 +0100 Subject: [PATCH] use GA class --- filter.pl | 88 +++++++++++++++++++++++++++++++++++++------------------ 1 file changed, 60 insertions(+), 28 deletions(-) diff --git a/filter.pl b/filter.pl index d2ebfaa..cb43a5c 100755 --- a/filter.pl +++ b/filter.pl @@ -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; -}