Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??

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.

edit: fixed bugs, which were verifying and printing the wrong versions; benchmark was ok


In reply to Re: Integer regex, different results in windows and mac - I just need regex help by pryrt
in thread Integer regex, different results in windows and mac - I just need regex help by hiyesthanks

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others avoiding work at the Monastery: (6)
As of 2024-04-24 09:19 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found