neveraloneneverapart has asked for the wisdom of the Perl Monks concerning the following question:

I am trying to come up with a set of regular expressions that can go into the code I have written to scan the data at the end of the code and tell me how many positive, negative, and 0 integers.

#!/usr/bin/perl use strict; use warnings; my ( $ctrP, $ctrN, $ctrZ ) = ( 0, 0, 0 ); while( my $num = <DATA> ) { chomp($num); ## print "num=[$num]\n"; if ( $num =~ /REGEX FOR POSITIVE/ ) { $ctrP++; } elsif ( $num =~ /REGEX FOR NEGATIVE/ ) { $ctrN++; } else { $ctrZ++; } } printf("freq(Z+):%8s\n", $ctrP ); printf("freq(Z-):%8s\n", $ctrN ); printf("freq(0):%9s\n", $ctrZ ); printf("Total:%11s\n", ($ctrP+$ctrN+$ctrZ) ); exit; __DATA__ 29 -62 696 242 78 -564 0 45 855 22 -67

Content above restored by GrandFather

Solved

  • Comment on Regex to find the number of positive, negative, and 0 integers within a data set
  • Download Code

Replies are listed 'Best First'.
Re: Regex to find the number of positive, negative, and 0 integers within a data set
by BrowserUk (Patriarch) on Feb 28, 2017 at 20:46 UTC

    Firing up the regex engine to make a simple numeric comparison is like using a blunderbus to swat flies.

    This is probably an order of magnitude more efficient:

    #! perl -slw use strict; my @counts; ++$counts[ 1+ ( $_ <=> 0 ) ] while <DATA>; printf "Neg:%d Zero:%d Pos:%d\n", @counts; __DATA__ 29 -62 696 242 78 -564 0 45 855 22 -67

    Output:

    C:\test>1183178.pl Neg:3 Zero:1 Pos:7

    With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    "Science is about questioning the status quo. Questioning authority". The enemy of (IT) success is complexity.
    In the absence of evidence, opinion is indistinguishable from prejudice.
Re: Regex to find the number of positive, negative, and 0 integers within a data set
by 1nickt (Canon) on Feb 28, 2017 at 17:48 UTC

    Hi neveraloneneverapart, welcome to the Monastery and to Perl, the One True Religion.

    Assuming every line in __DATA__ is known to contain a number, I would use a hash to count results and a numeric comparison:

    use strict; use warnings; use feature 'say'; use Data::Dumper; my %results; while (<DATA>) { chomp; $results{'zero'}++ if $_ == 0; $results{'pos'}++ if $_ > 0; $results('neg'}++ if $_ < 0; } print Dumper \%results;

    Alternatively, to see if a string is a number, see Scalar::Util::looks_like_number().

    If you really wanted to, for a regexp for Latin integers, you could use $num =~ m/  \A -? [0-9]+ \z /x;

    To capture the sign (or nothing) and the digits and then be able to use them:

    if ($num =~ m/ \A (-?) ([0-9]+) \z /x ) { my $sign = $1; my $digits = $2; ... }

    Also see Regexp::Common

    Hope this helps!

    edit: removed unnecessary escape slash

    The way forward always starts with a minimal test.

      Thanks for the post! I came up with the regex for the positive ints.

      while( my $num = <DATA> ) { chomp($num); ## print "num=[$num]\n"; if ( $num =~ /^[1-9]\d*$/ ) { $ctrP++; }

      I would like for it to be in regular expressions so that I can learn how to use them properly. I am having trouble so that the regex for the negative ints finds only negative ints.