Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

finding the number of digits in a string 1

Status
Not open for further replies.

danny2785

Programmer
Jun 26, 2006
16
0
0
US
Is there a way to find the number of digits that occur in a string without splitting the string into an array and then iterating through it?
 
Try this:
Code:
my $number_of_digits =  $test_string =~ tr/0-9/0-9/;
 
Ishnid, do you think you could step through the code for me? I don't get quite understand why that works. Just trying to learn.. Thanks
 
Code:
my $number_of_digits =  $test_string =~ tr/0-9/0-9/;

tr/_in_/_out_/ does a translation of characters and counts the number of translations it made. In this case, it swaps each number with itself (not changing the original string), but counts how many numbers it translated.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top