CORRECTION - Code simplification - round
This commit is contained in:
parent
2b6fdc74c4
commit
c8ae8bab6b
1 changed files with 35 additions and 24 deletions
|
|
@ -20,9 +20,9 @@ use Exporter;
|
|||
use strict;
|
||||
use Tie::IxHash;
|
||||
|
||||
our @ISA = qw(Exporter);
|
||||
our @EXPORT = qw(&makeTabSafe &makeArrayTabSafe &randomizeHash &commify &randomizeArray
|
||||
&sortHashDescending &sortHash &isIn &makeCommaSafe &makeArrayCommaSafe &randint &round);
|
||||
our @ISA = qw(Exporter);
|
||||
our @EXPORT = qw(&makeTabSafe &makeArrayTabSafe &randomizeHash &commify &randomizeArray
|
||||
&sortHashDescending &sortHash &isIn &makeCommaSafe &makeArrayCommaSafe &randint &round);
|
||||
|
||||
|
||||
=head1 NAME
|
||||
|
|
@ -71,8 +71,9 @@ Any old number will do.
|
|||
|
||||
=cut
|
||||
|
||||
|
||||
sub commify {
|
||||
my $text = reverse $_[0];
|
||||
my $text = reverse $_[ 0 ];
|
||||
$text =~ s/(\d\d\d)(?=\d)(?!\d*\.)/$1,/g;
|
||||
return scalar reverse $text;
|
||||
}
|
||||
|
|
@ -97,6 +98,7 @@ An array to look for the value in.
|
|||
|
||||
=cut
|
||||
|
||||
|
||||
sub isIn {
|
||||
my $key = shift;
|
||||
$_ eq $key and return 1 for @_;
|
||||
|
|
@ -119,9 +121,10 @@ A reference to the array to look through.
|
|||
|
||||
=cut
|
||||
|
||||
|
||||
sub makeArrayCommaSafe {
|
||||
my $array = $_[0];
|
||||
$_ = makeCommaSafe($_) for @$array;
|
||||
my $array = $_[ 0 ];
|
||||
$_ = makeCommaSafe( $_ ) for @$array;
|
||||
}
|
||||
|
||||
#-------------------------------------------------------------------
|
||||
|
|
@ -140,9 +143,10 @@ Searches through an array looking for tabs and replaces them with four spaces. A
|
|||
|
||||
=cut
|
||||
|
||||
|
||||
sub makeArrayTabSafe {
|
||||
my $array = $_[0];
|
||||
$_ = makeTabSafe($_) for @$array;
|
||||
my $array = $_[ 0 ];
|
||||
$_ = makeTabSafe( $_ ) for @$array;
|
||||
}
|
||||
|
||||
#-------------------------------------------------------------------
|
||||
|
|
@ -161,8 +165,9 @@ The text to search through.
|
|||
|
||||
=cut
|
||||
|
||||
|
||||
sub makeCommaSafe {
|
||||
my $text = $_[0];
|
||||
my $text = $_[ 0 ];
|
||||
$text =~ tr/,\r\n/; /;
|
||||
return $text;
|
||||
}
|
||||
|
|
@ -183,8 +188,9 @@ The text to search through.
|
|||
|
||||
=cut
|
||||
|
||||
|
||||
sub makeTabSafe {
|
||||
my $text = $_[0];
|
||||
my $text = $_[ 0 ];
|
||||
$text =~ tr/\r\n/ /;
|
||||
$text =~ s/\t/ /g;
|
||||
return $text;
|
||||
|
|
@ -210,11 +216,12 @@ The highest possible value. Defaults to 1.
|
|||
|
||||
=cut
|
||||
|
||||
|
||||
sub randint {
|
||||
my ($low, $high) = @_;
|
||||
$low = 0 unless defined $low;
|
||||
my ( $low, $high ) = @_;
|
||||
$low = 0 unless defined $low;
|
||||
$high = 1 unless defined $high;
|
||||
($low, $high) = ($high,$low) if $low > $high;
|
||||
( $low, $high ) = ( $high, $low ) if $low > $high;
|
||||
return $low + int( rand( $high - $low + 1 ) );
|
||||
}
|
||||
|
||||
|
|
@ -234,13 +241,14 @@ A reference to the array to randomize.
|
|||
|
||||
=cut
|
||||
|
||||
|
||||
sub randomizeArray {
|
||||
my $array = shift;
|
||||
if ($#$array > 0) {
|
||||
for (my $i = @$array; --$i; ) {
|
||||
my $j = int rand ($i+1);
|
||||
if ( $#$array > 0 ) {
|
||||
for ( my $i = @$array; --$i; ) {
|
||||
my $j = int rand( $i + 1 );
|
||||
next if $i == $j;
|
||||
@$array[$i,$j] = @$array[$j,$i];
|
||||
@$array[ $i, $j ] = @$array[ $j, $i ];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -261,13 +269,14 @@ A reference hash to randomize.
|
|||
|
||||
=cut
|
||||
|
||||
|
||||
sub randomizeHash {
|
||||
my $hash = $_[0];
|
||||
my $hash = $_[ 0 ];
|
||||
my @keys = keys %$hash;
|
||||
randomizeArray(\@keys);
|
||||
randomizeArray( \@keys );
|
||||
tie my %temp, 'Tie::IxHash';
|
||||
foreach my $key (@keys) {
|
||||
$temp{$key} = $hash->{$key};
|
||||
foreach my $key ( @keys ) {
|
||||
$temp{ $key } = $hash->{ $key };
|
||||
}
|
||||
return \%temp;
|
||||
}
|
||||
|
|
@ -288,8 +297,9 @@ Any floating point number.
|
|||
|
||||
=cut
|
||||
|
||||
|
||||
sub round {
|
||||
return sprintf('%.0f', $_[0]);
|
||||
return int( $_[ 0 ] + 0.5 );
|
||||
}
|
||||
|
||||
#-------------------------------------------------------------------
|
||||
|
|
@ -308,10 +318,11 @@ A hash to be sorted.
|
|||
|
||||
=cut
|
||||
|
||||
|
||||
sub sortHash {
|
||||
my %hash = @_;
|
||||
tie my %newHash, 'Tie::IxHash';
|
||||
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 };
|
||||
}
|
||||
return %newHash;
|
||||
|
|
@ -337,7 +348,7 @@ A hash to be sorted.
|
|||
sub sortHashDescending {
|
||||
my %hash = @_;
|
||||
tie my %newHash, 'Tie::IxHash';
|
||||
for my $key ( sort { $hash{$b} cmp $hash{$a} } keys %hash ) {
|
||||
for my $key ( sort { $hash{ $b } cmp $hash{ $a } } keys %hash ) {
|
||||
$newHash{ $key } = $hash{ $key };
|
||||
}
|
||||
return %newHash;
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue