You've already gotten good answers regarding CRLF, and chomping, and alternate regular expressions. However, to me, regex doesn't seem the best tool for the job. Testing whether a number is negative, zero, or positive seems a job for numerical operators to me. Using Test::More to verify and Benchmark to compare times, with the same set of 1000 random random integers:
use strict;
use warnings;
use Benchmark qw/cmpthese/;
use Test::More tests => 4;
my @DATA = map { int(-100 + rand 200) } 1 ... 1000;
my $g=use_grep(@DATA);
diag 'grep => ', explain $g;
my $c=use_comparison_ops(@DATA);
diag 'comparison_ops => ', explain $c;
my $s=use_spaceship_op(@DATA);
diag 'spaceship_op => ', explain $s;
my $r=use_hiyesthanks_regex(@DATA);
diag '[hiyesthanks] regex => ', explain $r;
my $m=use_marshall_regex(@DATA);
diag '[Marshall] regex => ', explain $m;
is_deeply $c, $g, 'verify comparison operators give same results as gr
+ep';
is_deeply $s, $g, 'verify spaceship operators give same results as gre
+p';
is_deeply $r, $g, 'verify [hiyesthanks] regex give same results as gre
+p';
is_deeply $m, $g, 'verify [Marshall] regex give same results as grep';
cmpthese( 0, {
use_grep => sub { use_hiyesthanks_regex(@DATA) },
use_hiyesthanks_regex => sub { use_hiyesthanks_regex(@DATA) },
use_marshall_regex => sub { use_marshall_regex(@DATA) },
use_comparison_ops => sub { use_comparison_ops(@DATA) },
use_spaceship_op => sub { use_spaceship_op(@DATA) },
});
sub use_hiyesthanks_regex {
my ( $count_of_positives, $count_of_negatives, $count_of_zeros, $c
+ount_of_others ) = ( 0, 0, 0, 0 );
for my $num ( @_ ) {
if ( $num =~ /^[0].{0}/ ) {
$count_of_zeros++;
}
elsif ( $num =~ /^\d[0-9]{1,3}$/ ) {
$count_of_positives++;
}
else {
$count_of_negatives++;
}
}
return {
count_of_positives => $count_of_positives,
count_of_negatives => $count_of_negatives,
count_of_zeros => $count_of_zeros,
count_of_others => $count_of_others,
total_count => $count_of_positives + $count_of_negativ
+es + $count_of_zeros + $count_of_others,
};
}
sub use_marshall_regex {
my ( $count_of_positives, $count_of_negatives, $count_of_zeros, $c
+ount_of_others ) = ( 0, 0, 0, 0 );
for my $num ( @_ ) {
if ( $num =~ /^0$/ ) {
$count_of_zeros++;
}
elsif ( $num =~ /^\d+$/ ) {
$count_of_positives++;
}
elsif ( $num =~ /^-\d+$/ ) {
$count_of_negatives++;
}
else {
$count_of_others++;
}
}
return {
count_of_positives => $count_of_positives,
count_of_negatives => $count_of_negatives,
count_of_zeros => $count_of_zeros,
count_of_others => $count_of_others,
total_count => $count_of_positives + $count_of_negativ
+es + $count_of_zeros + $count_of_others,
};
}
sub use_comparison_ops {
my ( $count_of_positives, $count_of_negatives, $count_of_zeros, $c
+ount_of_others ) = ( 0, 0, 0, 0 );
for my $num ( @_ ) {
if ( $num < 0 ) {
$count_of_negatives++;
}
elsif ( $num > 0 ) {
$count_of_positives++;
}
else {
$count_of_zeros++;
}
}
return {
count_of_positives => $count_of_positives,
count_of_negatives => $count_of_negatives,
count_of_zeros => $count_of_zeros,
count_of_others => $count_of_others,
total_count => $count_of_positives + $count_of_negativ
+es + $count_of_zeros + $count_of_others,
};
}
sub use_spaceship_op {
my ( $count_of_positives, $count_of_negatives, $count_of_zeros, $c
+ount_of_others ) = ( 0, 0, 0, 0 );
for my $num ( @_ ) {
for( $num <=> 0 ) {
$_ == -1 && do {$count_of_negatives++; next;};
$_ == +1 && do {$count_of_positives++; next;};
$_ == 0 && do {$count_of_zeros++; next;};
$count_of_others++;
}
}
return {
count_of_positives => $count_of_positives,
count_of_negatives => $count_of_negatives,
count_of_zeros => $count_of_zeros,
count_of_others => $count_of_others,
total_count => $count_of_positives + $count_of_negativ
+es + $count_of_zeros + $count_of_others,
};
}
sub use_grep {
my $count_of_positives = scalar grep { $_ > 0 } @_;
my $count_of_negatives = scalar grep { $_ < 0 } @_;
my $count_of_zeros = scalar grep { $_ == 0 } @_;
my $count_of_others = scalar(@_) - ($count_of_positives + $coun
+t_of_negatives + $count_of_zeros);
return {
count_of_positives => $count_of_positives,
count_of_negatives => $count_of_negatives,
count_of_zeros => $count_of_zeros,
count_of_others => $count_of_others,
total_count => $count_of_positives + $count_of_negativ
+es + $count_of_zeros + $count_of_others,
};
}
__END__
1..4
# grep => {
# 'count_of_negatives' => 479,
# 'count_of_others' => 0,
# 'count_of_positives' => 506,
# 'count_of_zeros' => 15,
# 'total_count' => 1000
# }
# comparison_ops => {
# 'count_of_negatives' => 479,
# 'count_of_others' => 0,
# 'count_of_positives' => 506,
# 'count_of_zeros' => 15,
# 'total_count' => 1000
# }
# spaceship_op => {
# 'count_of_negatives' => 479,
# 'count_of_others' => 0,
# 'count_of_positives' => 506,
# 'count_of_zeros' => 15,
# 'total_count' => 1000
# }
# [hiyesthanks] regex => {
# 'count_of_negatives' => 521,
# 'count_of_others' => 0,
# 'count_of_positives' => 464,
# 'count_of_zeros' => 15,
# 'total_count' => 1000
# }
# [Marshall] regex => {
# 'count_of_negatives' => 479,
# 'count_of_others' => 0,
# 'count_of_positives' => 506,
# 'count_of_zeros' => 15,
# 'total_count' => 1000
# }
ok 1 - verify comparison operators give same results as grep
ok 2 - verify spaceship operators give same results as grep
not ok 3 - verify [hiyesthanks] regex give same results as grep
# Failed test 'verify [hiyesthanks] regex give same results as grep'
# at C:\usr\local\share\passthru\perl\perlmonks\1201647.pl line 22.
# Structures begin differing at:
# $got->{count_of_positives} = '464'
# $expected->{count_of_positives} = '506'
ok 4 - verify [Marshall] regex give same results as grep
Rate use_marshall_regex use_hiyesthanks_regex
+ use_grep use_spaceship_op use_comparison_ops
use_marshall_regex 1715/s -- -18%
+ -26% -33% -84%
use_hiyesthanks_regex 2088/s 22% --
+ -9% -18% -81%
use_grep 2307/s 35% 10%
+ -- -9% -79%
use_spaceship_op 2543/s 48% 22%
+ 10% -- -77%
use_comparison_ops 10857/s 533% 420%
+ 371% 327% --
# Looks like you failed 1 test of 4.