This isn't "perfect", but gives an idea. I would use better variable names so that you have a chance to understand them 5 years from now.
#!usr/bin/perl # The program calculates the total zeros and positive integers from # the data using regex use strict; use warnings; my ( $c_positive_int, $c_negative_int, $c_zero_int, $ctr_not_int) = ( +0, 0, 0, 0 ); while( my $num = <DATA> ) { $num =~ s/^\s+//; # delete leading space(s) $num =~ s/\s+$//; # delete trailing space(s) if ( $num =~ /^0$/ ) # integer single 0 { $c_zero_int++; } elsif ( $num =~ /^\d+$/ ) # only positive digits { $c_positive_int++; } elsif ( $num =~ /^-\d+$/ ) #leading minus sign, then only digi +ts { $c_negative_int++; } else { $ctr_not_int++; #non integer print "**** Error $num is not an integer!\n"; } } printf "freq(Z+):%8i\n", $c_positive_int; printf "freq(Z-):%8i\n", $c_negative_int; printf "freq(0): %8i\n", $c_zero_int; printf "freq(?): %8i\n", $ctr_not_int; printf "Total: %8i\n", $c_positive_int + $c_negative_int + $c_zero_int + $ctr_not_int ; =Results **** Error 32.5 is not an integer! freq(Z+): 7 freq(Z-): 3 freq(0): 1 freq(?): 1 Total: 12 =cut __DATA__ 19 -22 498 512 15 -932 0 22 808 17 -32 32.5
In reply to Re: Integer regex, different results in windows and mac - I just need regex help
by Marshall
in thread Integer regex, different results in windows and mac - I just need regex help
by hiyesthanks
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |