Checking if a string contains all digits

paulbonugli
paulbonugli used Ask the Experts™
on
a nice easy one (I hope!) how can I check whether a string contains all digits? [could be of any length]

I know how to check for a digit at the start and the end, but I need to be able to prevent "7hello8" and things!
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
If a string contains only digits, it will match the pattern:

/^\d+$/

Author

Commented:
cheers jmbq... on a related topic (well, vaguely!) starting with 1, any ideas why the following algorithm would produce 1, 11, 111, 1111, 11111 etc. instead of the desired 1,2,3,4,5.

open (RECNUM,"+<$recordNum")
          $messageID = <RECNUM>;
          print RECNUM <RECNUM>+1;
     close (RECNUM)

Author

Commented:
tried using int(<recnum>) and that didn't solve the problem! any thoughts, anyone?
Exploring ASP.NET Core: Fundamentals

Learn to build web apps and services, IoT apps, and mobile backends by covering the fundamentals of ASP.NET Core and  exploring the core foundations for app libraries.


print RECNUM <RECNUM>+1;

i think your just appending 1 to recnum so if recnum is 1 the result will be 11

try:

open(RECNUM, "+<$recordNum");
   $messageID = <RECNUM>;
   print RECNUM <RECNUM>;
   $messageID = $messageID + 1;
close(RECNUM)

print RECNUM <RECNUM>+1;

i think your just appending 1 to recnum so if recnum is 1 the result will be 11

try:

open(RECNUM, "+<$recordNum");
   $messageID = <RECNUM>;
   print RECNUM <RECNUM>;
   $messageID = $messageID + 1;
close(RECNUM)
I think TheTyrant is on something of the right track, if a bit redundantly. Clearly you're getting the result you are getting because the +1 is getting treated as a string concatenation rather than an arithmetic operation. The method by which the string concatenation is occurring is a bit unusual though.

When you say

$messageID = <RECNUM>;

The entire contents of the file are copied to $messageID and the file pointer is left at the end of the file. When you say

print RECNUM <RECNUM>+1;

the result of <RECNUM> is undefined, since you were still at the end of the file, and that is treated as 0 when you add 1 to it. This "1" is then written to the file at the current position, adding just one more 1 to the file.

To get this to work more like you want, you need to reposition the file pointer. Add the line

seek RECNUM, 0, 0; # rewind

in between the reading of the file and the print statement; don't read the file in the print statement, use the value you retrieved.


     open RECNUM,"+<$recordNum";
      $messageID = <RECNUM>;
      seek RECNUM, 0, 0; # rewind
      print RECNUM $messageID+1;
     close RECNUM;

In other situations where you're updating, a 'truncate' operation might be needed, but the number being written will always be the same size or larger than what was there before, so no truncation is necessary. You'll also want to use 'flock' after the open if this is being used in a situation where there could possibly be more than one script attempting to access the file simultaneously.
Why don't you just try like:

if ($string =~ m/\D/g) { print "There's a char in the string";
} else { #command }
And for the second Question try:

chop ($num = <STDIN>);
$add = $num;
while () {
print "$num, ";
$num .= $add;
}

This would produce your desired sequence.

Author

Commented:
thanks jmcq: your solution (both of them!) worked perfectly. I think it must have been that it was simply appending rather than actually incrementing and overwriting.

thanks to everyone else for your input.

P.S. I am using flock and everything - I just wanted to simplify the code when posting on here!

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial