Post Reply 
VBS - Read Certain Characters
Mar. 18, 2004, 04:05 AM
Post: #1
 
Hey guys. How can I read certain characters from an InputBox. I need to take the different numbers that the user eneters into the InputBox and do math with them. For example, if some entered in the input box '0123456789' I want to have it take like the sixth number in that string and multiply it by 3 so the number would now be '01234156789'. How can I do something similar to that example if its even possible?

�{=(~�::[Shea]::��~)=}�
How 'bout you sideburns, you want some of this milk?
This fading text is pretty cool, eh? I bet you wish you had some.
Add Thank You Quote this message in a reply
Mar. 18, 2004, 05:30 AM
Post: #2
 
Shea,

Simple. Dim an array, use the int data type, read from the input box to fill the array, and then count off the index to get to the digit you want. Be sure to start your count at 0 (zero), not 1 (one).

But what happens if the user enters a series where one of the numbers is larger than one digit? The way you showed the series of numbers, there's no way to distinguish a single digit number from a multi-digit number, so the user is hosed. The short solution is to warn the user that only 0 thru 9 are acceptable. But the flip side is that you can't put your new number back into the same array if it's greater than 9, so you're on to Plan B.

There is a slightly messy way around this limit, and that is to use a two-dimensional array. But you have to persuade the user to separate his/her numbers with a space, a comma, or something. Once that's done, you're home free. Reading the series into the array is a bit of a pain, but it can be done.

I'm not sure what you're intending to accomplish, but one thing that I've seen before with this kind of manipulation is date calculations. That's a different story. Use the date type instead of the int type, and you can easily do arithmetic on that, then replace it back into the array, no sweat.

Does this help?


Oddysey

I'm no longer in the rat race - the rats won't have me!
Add Thank You Quote this message in a reply
Mar. 18, 2004, 01:47 PM
Post: #3
 
I don't think I have to have them separate them by spaces. I'm making a number validator that uses the Luhn algorithm. For those who don't know what that here's a brief explanation:

Th Luhn algorithm is used to validate credit card numbers at banks and online stores. It is very simple, but effective. What you do is take lets say a 13 number credit card number (4682734907632). You take the second to last number and double it. You do that to every other number moving left. So it would be: 4(6*2)8(2*2)7(3*2)4(9*2)0(7*2)6(3*2)2 or 4 12 8 4 7 6 4 18 0 14 6 6 2. Then you add each digit up, breaking up the 2 digit numbers. 4+1+2+8+4+7+6+4+1+8+0+1+4+6+6+2 or 64. That is invalid. When you add all the numbers up it has to equal a number that ends in a 0.

Because of the first formula where you multiply everyother number by 2 this might be impossible with VBS because after you multiply them you don't know how many digits there are now. Is there a way to count the number of digits in a variable?

�{=(~�::[Shea]::��~)=}�
How 'bout you sideburns, you want some of this milk?
This fading text is pretty cool, eh? I bet you wish you had some.
Add Thank You Quote this message in a reply
Mar. 23, 2004, 08:52 AM
Post: #4
 
Shea;
Quote:Because of the first formula where you multiply everyother number by 2 this might be impossible with VBS because after you multiply them you don't know how many digits there are now. Is there a way to count the number of digits in a variable?
So grasshopper, you have forgotten your times tables already? Sad No single digit multiplied by 2 can be larger than two digits - in fact, the largest number will be only 18 (2 x 9).

Now that you've explained what we're doing here, I can cut to the chase.

It won't matter how many digits are in a variable. Simply convert each number into a character (or character pair), and successively stuff them into one long string. Then peel each character off that string, convert it back to an int, and do your addition. Let the conversion to char and back to int be your "break apart" algorithm for multi-digit numbers.

Still feel the need to count digits in a variable? (I don't know of any such method, but you could do the conversion as above, then check the string length.) However, why do you need to know in advance the number of single digits you're going to be adding up? Just DIM your char array ahead of time for 24 chars (what used to be digits), and let the chips fall where they may. When it comes time to add everything up, peel off each char, convert, and add. No harm done if the array isn't full to start with, right? Just be sure to check for the NUL char to determine that you are at the end of your chars/digits, so that you don't add undefined chars to your magic number.


Oddysey

I'm no longer in the rat race - the rats won't have me!
Add Thank You Quote this message in a reply
Mar. 23, 2004, 01:24 PM
Post: #5
 
Thanx Oddysey that really helped! I'll get back to you when I get it working.

�{=(~�::[Shea]::��~)=}�
How 'bout you sideburns, you want some of this milk?
This fading text is pretty cool, eh? I bet you wish you had some.
Add Thank You Quote this message in a reply
Post Reply 


Forum Jump: