Code simplifications.

This commit is contained in:
John W. Krahn 2003-01-12 17:00:26 +00:00
parent 25155bf375
commit 8c75921e08

View file

@ -72,9 +72,9 @@ Any old number will do.
=cut =cut
sub commify { sub commify {
my $text = reverse $_[0]; my $text = reverse $_[0];
$text =~ s/(\d\d\d)(?=\d)(?!\d*\.)/$1,/g; $text =~ s/(\d\d\d)(?=\d)(?!\d*\.)/$1,/g;
return scalar reverse $text; return scalar reverse $text;
} }
#------------------------------------------------------------------- #-------------------------------------------------------------------
@ -99,8 +99,8 @@ An array to look for the value in.
sub isIn { sub isIn {
my $key = shift; my $key = shift;
$_ eq $key and return 1 for @_; $_ eq $key and return 1 for @_;
return 0; return 0;
} }
#------------------------------------------------------------------- #-------------------------------------------------------------------
@ -120,11 +120,8 @@ A reference to the array to look through.
=cut =cut
sub makeArrayCommaSafe { sub makeArrayCommaSafe {
my ($array) = $_[0]; my $array = $_[0];
my ($i); $_ = makeCommaSafe($_) for @$array;
for ($i = @$array; --$i;) {
$$array[$i] = makeCommaSafe($$array[$i]);
}
} }
#------------------------------------------------------------------- #-------------------------------------------------------------------
@ -144,11 +141,8 @@ Searches through an array looking for tabs and replaces them with four spaces. A
=cut =cut
sub makeArrayTabSafe { sub makeArrayTabSafe {
my ($array) = $_[0]; my $array = $_[0];
my ($i); $_ = makeTabSafe($_) for @$array;
for ($i = @$array; --$i;) {
$$array[$i] = makeTabSafe($$array[$i]);
}
} }
#------------------------------------------------------------------- #-------------------------------------------------------------------
@ -168,11 +162,9 @@ The text to search through.
=cut =cut
sub makeCommaSafe { sub makeCommaSafe {
my ($text) = $_[0]; my $text = $_[0];
$text =~ s/\n/ /g; $text =~ tr/,\r\n/; /;
$text =~ s/\r/ /g; return $text;
$text =~ s/,/;/g;
return $text;
} }
#------------------------------------------------------------------- #-------------------------------------------------------------------
@ -192,11 +184,10 @@ The text to search through.
=cut =cut
sub makeTabSafe { sub makeTabSafe {
my ($text) = $_[0]; my $text = $_[0];
$text =~ s/\n/ /g; $text =~ tr/\r\n/ /;
$text =~ s/\r/ /g; $text =~ s/\t/ /g;
$text =~ s/\t/ /g; return $text;
return $text;
} }
#------------------------------------------------------------------- #-------------------------------------------------------------------
@ -244,11 +235,10 @@ A reference to the array to randomize.
=cut =cut
sub randomizeArray { sub randomizeArray {
my ($array, $i, $j); my $array = shift;
$array = shift;
if ($#$array > 0) { if ($#$array > 0) {
for ($i = @$array; --$i; ) { for (my $i = @$array; --$i; ) {
$j = int rand ($i+1); my $j = int rand ($i+1);
next if $i == $j; next if $i == $j;
@$array[$i,$j] = @$array[$j,$i]; @$array[$i,$j] = @$array[$j,$i];
} }
@ -272,14 +262,11 @@ A reference hash to randomize.
=cut =cut
sub randomizeHash { sub randomizeHash {
my ($hash, $key, @keys, %temp); my $hash = $_[0];
$hash = $_[0]; my @keys = keys %$hash;
foreach $key (keys %{$_[0]}) {
push(@keys,$key);
}
randomizeArray(\@keys); randomizeArray(\@keys);
tie %temp, 'Tie::IxHash'; tie my %temp, 'Tie::IxHash';
foreach $key (@keys) { foreach my $key (@keys) {
$temp{$key} = $hash->{$key}; $temp{$key} = $hash->{$key};
} }
return \%temp; return \%temp;
@ -302,7 +289,7 @@ Any floating point number.
=cut =cut
sub round { sub round {
return sprintf("%.0f", $_[0]); return sprintf('%.0f', $_[0]);
} }
#------------------------------------------------------------------- #-------------------------------------------------------------------
@ -322,14 +309,12 @@ A hash to be sorted.
=cut =cut
sub sortHash { sub sortHash {
my ( %hash, %newHash ); my %hash = @_;
tie %hash, "Tie::IxHash"; tie my %newHash, 'Tie::IxHash';
tie %newHash, "Tie::IxHash";
%hash = @_;
for my $key ( sort { $hash{$a} cmp $hash{$b} } keys %hash ) { for my $key ( sort { $hash{$a} cmp $hash{$b} } keys %hash ) {
$newHash{ $key } = $hash{ $key }; $newHash{ $key } = $hash{ $key };
} }
return %newHash; return %newHash;
} }
#------------------------------------------------------------------- #-------------------------------------------------------------------
@ -350,14 +335,12 @@ A hash to be sorted.
sub sortHashDescending { sub sortHashDescending {
my ( %hash, %newHash ); my %hash = @_;
tie %hash, "Tie::IxHash"; tie my %newHash, 'Tie::IxHash';
tie %newHash, "Tie::IxHash"; for my $key ( sort { $hash{$b} cmp $hash{$a} } keys %hash ) {
%hash = @_; $newHash{ $key } = $hash{ $key };
for my $key ( sort { $hash{$b} cmp $hash{$a} } keys %hash ) { }
$newHash{ $key } = $hash{ $key }; return %newHash;
}
return %newHash;
} }
1; 1;