Using variable values within other variables (Language Help)

by Lockjaw, Monday, June 04, 2012, 23:52 (1754 days ago)

I'm working on a program that needs to use the incremental value of a for to loop in another variable name, like this:

for ddi=1 to 10
dc(ddi)$=mid(string(wcheck),ddi,1)
dcsum=dcsum+int(dc(ddi)$)
next ddi

What I'm trying to do is take a ten-digit number (wcheck) and add the digits together to check against a checksum. Is there a way to make the value of ddi part of another variable (dc1$-dc10$)? Or is there an easier way of doing this rather than converting the number to a string, stripping the numbers with the mid statement, then converting them back to add them?

Thanks!

Using variable values within other variables

by Jim ⌂ @, Russell, KY, Tuesday, June 05, 2012, 15:38 (1754 days ago) @ Lockjaw

Have you thought about doing it mathematically (this does it backwards but will give fast results)

wcheck = ????
t = wcheck
dcsum = 0
for ddi = 1 to 10
dcsum = t mod 10
t = dcsum \ 10
next ddi

Using variable values within other variables

by Lockjaw, Tuesday, June 05, 2012, 17:53 (1754 days ago) @ Jim

That didn't quite work Jim, but it put me on the right path. Here's what I did (oh, and I lied - it's only an eight digit number):


wcheck = 11148470
t = float(wcheck)/10^8
dcsum = 0
for ddi = 1 to 7
t=float(t*10)
dcsum = dcsum + t%10
t = t-(t%10)
if t>0 then
next ddi
endif
print dcsum

Thanks for heading me in the right direction! I'd still like to find a way to use variable values as part of another variable though, like this:

for i=1 to 10
d(i) = i
next i

The end result I want is

d1 = 1
d2 = 2
etc.

Any way to do this?

Using variable values within other variables

by Jim ⌂ @, Russell, KY, Friday, June 08, 2012, 04:58 (1751 days ago) @ Lockjaw

Why don't you just do it with an array?

dim d(10)
for i=1 to 10
d[i-1] = i
next i

Using variable values within other variables

by Lockjaw, Friday, June 08, 2012, 17:08 (1751 days ago) @ Jim

Brilliant! Hadn't thought of that!

On the same note, here's a bit of an anomaly I've discovered. When I run this bit of code:

wcheck = 11148470
t = float(wcheck)/10^8
dcsum = 0
for ddi = 1 to 7
t=float(t*10)
dcsum = dcsum + t%10
t = t-(t%10)
if t>0 then
next ddi
endif
print dcsum

it treats the 4 as a 3. So if wcheck = 44444444, it gives me a 31 instead of 32. It does the same thing if I use a 3 in the fourth position, it treats it as a 2. All other digits are okay. Any idea why this is happening? I've run it on WinNT, XP & 7 with the same result.

Using variable values within other variables

by Jim ⌂ @, Russell, KY, Friday, June 15, 2012, 14:59 (1744 days ago) @ Lockjaw

Sorry it took a few days to respond.

Under UBUNTU/LINUX the code works just as you like it. As an old programmer I do not trust the modulo operator if either side is a floating point. :) I guess there could be a rounding or floating point problem.

I would implement this as a string of digits so there would be no way of getting all "floaty" on us.

# wcheck = 44444444
# wcheck$ = string(wcheck) # different variable - same name almost but a string
# or
wcheck$ = "21312312312345342341234324"

dcsum = 0
for i = 1 to length(wcheck$)
dcsum = dcsum + int(mid(wcheck$,i,1))
next i
print dcsum

RSS Feed of thread
powered by my little forum