Problem with loop

Problem with loop

am 29.12.2006 15:54:49 von kim

Hi, why doesn't this work inside a loop, gives me the wrong "kontrollnummer"
(increases it as the loop goes on)

<%
Dim kontrollnummer
Dim raknare
Dim Summa
Dim referens
referens = (rs_faktura.Fields.Item("customer_ID").Value) &
(rs_faktura.Fields.Item("bill_ID").Value)

Dim faktor
faktor = 7

For raknare = Len(referens) To 1 Step -1
Summa = Summa + Mid(referens, raknare, 1) * faktor
If faktor = 7 Then
faktor = 3
ElseIf faktor = 3 Then
faktor = 1
ElseIf faktor = 1 Then
faktor = 7
End If
Next

kontrollnummer = Right((10 - (Summa Mod 10)), 1)
%>
<%=kontrollnummer%>

Re: Problem with loop

am 29.12.2006 18:19:35 von reb01501

Kim wrote:
> Hi, why doesn't this work inside a loop, gives me the wrong
> "kontrollnummer" (increases it as the loop goes on)
>
> <%
> Dim kontrollnummer
> Dim raknare
> Dim Summa
> Dim referens
> referens = (rs_faktura.Fields.Item("customer_ID").Value) &
> (rs_faktura.Fields.Item("bill_ID").Value)
>
> Dim faktor
> faktor = 7
>
> For raknare = Len(referens) To 1 Step -1
> Summa = Summa + Mid(referens, raknare, 1) * faktor
> If faktor = 7 Then
> faktor = 3
> ElseIf faktor = 3 Then
> faktor = 1
> ElseIf faktor = 1 Then
> faktor = 7
> End If
> Next
>
> kontrollnummer = Right((10 - (Summa Mod 10)), 1)
> %>
> <%=kontrollnummer%>

Show us:
1. what the inputs look like
2. what the output is supposed to look like
3. what the output you are actually getting looks like

--
Microsoft MVP - ASP/ASP.NET
Please reply to the newsgroup. This email account is my spam trap so I
don't check it very often. If you must reply off-line, then remove the
"NO SPAM"

Re: Problem with loop

am 29.12.2006 18:48:47 von kim

> Show us:
> 1. what the inputs look like
> 2. what the output is supposed to look like
> 3. what the output you are actually getting looks like

Input looks like forinstance 42146 (customer id 42 & bill id 146). Based on
this I need a check digit to the end at this 42146 that is calculated like:

the digits to be verified are multiplied by the weights 7, 3, 1, 7, 3,1 .
from the right to left. The multiplied sums are added up and the sum is
subtracted from then nearest figure ending in zero. The resulting difference
is the check digit which is entered as the last digit in the reference
number. If the difference is 10, the check digit is 0.

This is calculated properly by the code in my initial post if done once. But
when I put this into a loop only the first time are calculated properly,
thereafter the check digits turns out wrong.

Re: Problem with loop

am 29.12.2006 18:59:47 von reb01501

Kim wrote:
>> Show us:
>> 1. what the inputs look like
>> 2. what the output is supposed to look like
>> 3. what the output you are actually getting looks like
>
> Input looks like forinstance 42146 (customer id 42 & bill id 146).
> Based on this I need a check digit to the end at this 42146 that is
> calculated like:
> the digits to be verified are multiplied by the weights 7, 3, 1, 7,
> 3,1 . from the right to left. The multiplied sums are added up and
> the sum is subtracted from then nearest figure ending in zero. The
> resulting difference is the check digit which is entered as the last
> digit in the reference number. If the difference is 10, the check
> digit is 0.
> This is calculated properly by the code in my initial post if done
> once. But when I put this into a loop only the first time are
> calculated properly, thereafter the check digits turns out wrong.

Again, please show us the inputs from say, the first three records, show us
what the desired output is supposed to look like from those three pieces of
data, and what you are actually getting. Looking at this, combined with your
above description, will help us analyze your code and figure out the
problem. A picture is worth a thousand words.


--
Microsoft MVP - ASP/ASP.NET
Please reply to the newsgroup. This email account is my spam trap so I
don't check it very often. If you must reply off-line, then remove the
"NO SPAM"

Re: Problem with loop

am 29.12.2006 19:19:49 von kim

> Again, please show us the inputs from say, the first three records, show
> us what the desired output is supposed to look like from those three
> pieces of data, and what you are actually getting. Looking at this,
> combined with your above description, will help us analyze your code and
> figure out the problem. A picture is worth a thousand words.

First record:
input = 3295
output= 32955 (ok)

second record:
input= 4296
output= 42966 (false, should be 42961)

third record:
input= 6297
output= 62976 (false, should be 62970)

Re: Problem with loop

am 29.12.2006 20:59:07 von reb01501

Kim wrote:
>> Again, please show us the inputs from say, the first three records,
>> show us what the desired output is supposed to look like from those
>> three pieces of data, and what you are actually getting. Looking at
>> this, combined with your above description, will help us analyze
>> your code and figure out the problem. A picture is worth a thousand
>> words.
>
> First record:
> input = 3295
> output= 32955 (ok)
>
> second record:
> input= 4296
> output= 42966 (false, should be 42961)
>
> third record:
> input= 6297
> output= 62976 (false, should be 62970)

OK, I'm back. I will play with this and get back to you.

--
Microsoft MVP - ASP/ASP.NET
Please reply to the newsgroup. This email account is my spam trap so I
don't check it very often. If you must reply off-line, then remove the
"NO SPAM"

Re: Problem with loop

am 29.12.2006 21:31:54 von reb01501

This is a multi-part message in MIME format.

------=_NextPart_000_00F7_01C72B5E.7845F7C0
Content-Type: text/plain;
charset="Windows-1252"
Content-Transfer-Encoding: quoted-printable

Kim wrote:
>> Again, please show us the inputs from say, the first three records,
>> show us what the desired output is supposed to look like from those
>> three pieces of data, and what you are actually getting. Looking at
>> this, combined with your above description, will help us analyze
>> your code and figure out the problem. A picture is worth a thousand
>> words.=20
>=20
> First record:
> input =3D 3295
> output=3D 32955 (ok)
>=20
> second record:
> input=3D 4296
> output=3D 42966 (false, should be 42961)
>=20
> third record:
> input=3D 6297
> output=3D 62976 (false, should be 62970)

OK, I could not use a recordset of course, so I put your inputs into an =
array and ran your code to get your results. I then did this to help you =
see what was going on:
<%
Dim kontrollnummer
Dim raknare
Dim Summa
Dim referens
dim ar, i
Dim faktor
ar=3Darray("3295","4296","6297")
for i =3D 0 to 2
referens =3D ar(i)
faktor =3D 7
Response.Write "


" & referens & "
"
Response.Write ""
For raknare =3D Len(referens) To 1 Step -1
Response.Write ""
Response.Write ""
Response.Write ""
Summa =3D Summa + Mid(referens, raknare, 1) * faktor
Response.Write ""
=20
If faktor =3D 7 Then
faktor =3D 3
ElseIf faktor =3D 3 Then
faktor =3D 1
ElseIf faktor =3D 1 Then
faktor =3D 7
End If
Next
Response.Write "
raknarefactor" & _
"
Summa (pre)Summa (post)
" & raknare & "" & faktor & "" & Summa & "" & Summa & "
"
kontrollnummer =3D Right((10 - (Summa Mod 10)), 1)
Response.Write kontrollnummer & "
"
next

%>
Running this code yields this result:

------------------------------------------------------------ -------------=
-------

3295

raknare factor Summa (pre) Summa (post)=20
4 7 35=20
3 3 35 62=20
2 1 62 64=20
1 7 64 85=20

5


------------------------------------------------------------ -------------=
-------

4296

raknare factor Summa (pre) Summa (post)=20
4 7 85 127=20
3 3 127 154=20
2 1 154 156=20
1 7 156 184=20

6


------------------------------------------------------------ -------------=
-------

6297

raknare factor Summa (pre) Summa (post)=20
4 7 184 233=20
3 3 233 260=20
2 1 260 262=20
1 7 262 304=20

6

As you should see from this, the problem is the failure to initialize =
Summa before each loop. Adding "Summa =3D 0" to the code, right below =
the "faktor=3D7" line yields the correct results:

------------------------------------------------------------ -------------=
-------
3295
raknare factor Summa (pre) Summa (post)=20
4 7 0 35=20
3 3 35 62=20
2 1 62 64=20
1 7 64 85=20
5

------------------------------------------------------------ -------------=
-------
4296
raknare factor Summa (pre) Summa (post)=20
4 7 0 42=20
3 3 42 69=20
2 1 69 71=20
1 7 71 99=20
1

------------------------------------------------------------ -------------=
-------
6297
raknare factor Summa (pre) Summa (post)=20
4 7 0 49=20
3 3 49 76=20
2 1 76 78=20
1 7 78 120=20
0




--=20
Microsoft MVP - ASP/ASP.NET
Please reply to the newsgroup. This email account is my spam trap so I
don't check it very often. If you must reply off-line, then remove the
"NO SPAM"
------=_NextPart_000_00F7_01C72B5E.7845F7C0
Content-Type: text/html;
charset="Windows-1252"
Content-Transfer-Encoding: quoted-printable



charset=3Dwindows-1252">




Kim wrote:
>> Again, please show us the =
inputs from=20
say, the first three records,
>> show us what the desired =
output is=20
supposed to look like from those
>> three pieces of data, and =
what you=20
are actually getting. Looking at
>> this, combined with your =
above=20
description, will help us analyze
>> your code and figure out =
the=20
problem. A picture is worth a thousand
>> words.
> =

>=20
First record:
> input =3D     3295
>=20
output=3D    32955 (ok)
>
> second =
record:
>=20
input=3D        4296
>=20
output=3D      42966 (false, should be =
42961)
>=20

> third record:
> =
input=3D       =20
6297
> output=3D      62976 (false, =
should be=20
62970)

 

OK, I could not use a recordset of course, so I put =
your=20
inputs into an array and ran your code to get your results. I then did =
this to=20
help you see what was going on:

<%
  Dim kontrollnummer
  Dim=20
raknare
  Dim Summa
  Dim referens
  dim ar, =
i
 =20
Dim faktor
  ar=3Darray("3295","4296","6297")
  for i =
=3D 0 to=20
2
  referens =3D ar(i)
  faktor =
7
  Response.Write "<hr><b>" & referens =
&=20
"</b><br>"
  Response.Write=20
"<TABLE><tr><th>raknare</th><th>factor" =
&=20
_
  "</th><th>Summa =
(pre)</th><th>Summa=20
(post)</th></tr>"
  For raknare =3D =
Len(referens) To 1=20
Step -1
   Response.Write "<tr><td>" & =
raknare=20
& "</td>"
   Response.Write "<td>" =
&=20
faktor & "</td>"
   Response.Write =
"<td>"=20
& Summa & "</td>"
   Summa =3D Summa +=20
Mid(referens, raknare, 1) * faktor
   Response.Write=20
"<td>" & Summa &=20
"</td></tr>"
   
   If =
faktor =
7 Then
    faktor =3D =
3
   ElseIf faktor=20
=3D 3 Then
    faktor =3D =
1
   ElseIf=20
faktor =3D 1 Then
    faktor =3D =
7
   End=20
If
  Next
  Response.Write=20
"</table>"
  kontrollnummer =3D Right((10 - (Summa =
Mod 10)),=20
1)
  Response.Write kontrollnummer & =
"<BR>"
 =20
next

 

  %>
Running this code yields this=20
result:





3295




























raknare factor Summa (pre) Summa (post)
4 7 35
3 3 35 62
2 1 62 64
1 7 64 85

5





4296




























raknare factor Summa (pre) Summa (post)
4 7 85 127
3 3 127 154
2 1 154 156
1 7 156 184

6





6297




























raknare factor Summa (pre) Summa (post)
4 7 184 233
3 3 233 260
2 1 260 262
1 7 262 304

6

As you should see from this, the problem is the =
failure to=20
initialize Summa before each loop. Adding "Summa =3D 0" to the code, =
right below=20
the "faktor=3D7" line yields the correct results:




3295



























raknare factor Summa (pre) Summa (post)
4 7 0 35
3 3 35 62
2 1 62 64
1 7 64 85
5



4296



























raknare factor Summa (pre) Summa (post)
4 7 0 42
3 3 42 69
2 1 69 71
1 7 71 99
1



6297



























raknare factor Summa (pre) Summa (post)
4 7 0 49
3 3 49 76
2 1 76 78
1 7 78 120
0

 



--
Microsoft MVP - ASP/ASP.NET
Please reply to the=20
newsgroup. This email account is my spam trap so I
don't check it =
very often.=20
If you must reply off-line, then remove the
"NO SPAM" =20


------=_NextPart_000_00F7_01C72B5E.7845F7C0--

Re: Problem with loop

am 29.12.2006 21:47:22 von kim

Oh man, that was an impressive answer. Not only did it work perfectly, it
also helped me understand the problem. Thank you so much, Bob.