Do While until variable is empty/null - vbscript

Not sure if this is possible:
myString = 313233
counter = Len(myString)
Do while counter > 0
position = 1
take = 2
response.write (" counter:" & counter)
first_two = Mid(myString, position, take)
response.write (" Each loop:" & first_two)
position = position + 2
counter = counter - 2
Loop
Can someone tell me where I'm going wrong please? or is this possible via different method?
The code above is running once and returning: 31 not 31 32 33
Thanks.
UPDATE: Realized my error shortly after but now position won't increment to give to move up the variable

You set the position variable to 1 at the start of the Do loop. As it stands whenever Mid() is called it is passed position = 1 which will return 31.
Fix this by moving it outside the loop so position can increment.
myString = 313233
counter = Len(myString)
'Set before start of loop
position = 1
take = 2
Do while counter > 0
response.write (" counter:" & counter)
first_two = Mid(myString, position, take)
response.write (" Each loop:" & first_two)
'Use take variable so increment is always the same.
position = position + take
counter = counter - take
Loop
Code untested

Related

Edit - How to fix this if else statement variable issue?

I'm trying to make a random number game but the condition is always false even though I added the b = input box statement
Option Explicit
dim b,a,max,min
'To randomize variable (a)
max=3
min=1
Randomize
a = (Int((max-min+1)*Rnd+min))
b = inputbox("Guess a number from " & min & " to " & max)
If a = b Then
msgbox("you win")
Else
msgbox("you died it was " & a)
End If
I expected when you guessed the right number it would say you when but it always you died the number was #
You are almost there but as has been mentioned in the comments you do not populate the variable be with a values so the comparison will always be False.
If you are expecting b to be populated by the user you could ask for input via the InputBox() function by adding one line;
Option Explicit
Dim beans, b, a, max, min
'To randomize variable (a)
max = 100
min = 1
Call Randomize()
'Enter the line below to collect input from the user.
b = InputBox("Enter a number between " & min & " and " & max & ".")
'Remember to round the number to make sure you have a whole number.
a = Round((Int((max - min + 1) * Rnd() + min)))
If (a = b) Then
Call MsgBox("You win")
Else
Call MsgBox("You died it was " & a)
End If
You might also consider validating the input to make sure that the user enters a value between your min and max and responding accordingly if the value is invalid.
This matches 1 - 10.
Randomize
Num = Int((10 - 1 + 1) * Rnd + 1)
If CInt(Inputbox("Enter Number")) = Num Then
Msgbox "match"
Else
Msgbox "Nope it was " & Num
End If
The formula from help is Int((upperbound - lowerbound + 1) * Rnd + lowerbound). See http://download.microsoft.com/download/winscript56/Install/5.6/W982KMeXP/EN-US/scrdoc56en.exe.

splitting a string in chunks using classic asp

i got a list coma separated values (a,b,c,d,e,f,g,h,....)
i wish to split them into chunks of 5 like (a,b,c,d,e) (f,g,h,i,j)....
can someone help me with the code in classic asp ?
arr = Split(messto, ",") ' convert to array
totalemails = UBound(arr) ' total number of emails
if totalemails mod 5 = 0 then
totalloops = int(totalemails/5)
else
totalloops = int(totalemails/5) + 1
end if
x = 0
y = 0
b = 0
for x = 0 to totalloops
for counter = (5* x) to ((b+5)-1)
if Trim(arr(counter)) <> "" and isnull(trim(arr(counter))) = false then
response.Write(Trim(arr(counter)))
response.Write(counter & "<br>")
mymssto = mymssto & Trim(arr(counter)) & ","
response.Write(mymssto)
end if
next
You want to use Mod() to do this it's very powerful and underutilised function.
Here is a simple example based on the code in the question;
<%
Dim mumberToGroupBy: numberToGroupBy = 5
Dim index, counter, arr, messto
messto = "a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q"
arr = Split(messto, ",") ' convert to array
For counter = 0 To UBound(arr)
'Can't divide by 0 so we need to make sure our counter is 1 based.
index = counter + 1
Call Response.Write(Trim(arr(counter)))
'Do we have any remainder in the current grouping?
If index Mod numberToGroupBy = 0 Then Response.Write("<br>")
Next
%>
Output:
abcde
fghij
klmno
pq
Useful Links
A: Change response to only respond one set of values (details the use of Mod())

iF Then Else code - how to make this run faster? VBA

I have a simple code which takes a long time to run. I was wondering if there is any way to make this run faster? Maybe this part (Cells(i, "U").Value = Cells(n, "X").Value) should not be used 2 times! Thanks!
For n = 3 To time_frame + 3
For i = 3 To 1002
If (Cells(i, "U").Value = Cells(n, "X").Value) And (Bed_in_use < 24) And Wait_L > 0 Then
Wait_L = Wait_L - (24 - Bed_in_use)
ElseIf (Cells(i, "U").Value = Cells(n, "X").Value) And (Bed_in_use < 24) And Wait_L <= 0 Then
Bed_in_use = Bed_in_use + 1
End If
Next i
Next n
MsgBox "The number of bed in use is " & Bed_in_use & ". There are " & Wait_L & " patients in the waiting list."
End Sub
Couple things will speed this up - The first was mentioned in the comments by #jcarroll, pulling the cells you need into an array and using that instead of making repeated calls to Cells.
The second is what you mentioned, structuring your If statements in a way that
you aren't making the same comparisons twice. For example, this has to be true for either condition...
Cells(i, "U").Value = Cells(n, "X").Value
...and this always has to be true:
Bed_in_use < 24
After Bed_in_use is 24 (or higher), you can exit out of the loop because you'll never satisfy either the If or the ElseIf statement. I'd re-roll it into something like this:
Dim values() As Variant
values = ActiveSheet.UsedRange '...or whatever Range you need.
For n = 3 To time_frame + 3
If Bed_in_use >= 24 Then Exit For
For i = 3 To 1002
If Bed_in_use >= 24 Then Exit For
If values(i, 21).Value = values(n, 24).Value Then
If Wait_L > 0 Then
Wait_L = Wait_L - (24 - Bed_in_use)
Else
Bed_in_use = Bed_in_use + 1
End If
End If
Next i
Next n
I'm not totally sure what your code is trying to do. But here is a sample of how you would compare two lists, and keep track of the total matches.
Sub test()
Dim arrayU() As Variant
Dim arrayX() As Variant
Dim LrowU As Integer
Dim LrowX As Integer
Dim i As Integer
Dim j As Integer
Dim bed_in_use As Integer
LrowU = Columns(21).Find(What:="*", LookIn:=xlValues, SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
LrowX = Columns(24).Find(What:="*", LookIn:=xlValues, SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
ReDim arrayU(1 To LrowU)
ReDim arrayX(1 To LrowX)
For i = 1 To LrowU
arrayU(i) = Cells(i, 21)
Next i
i = 1
For i = 1 To LrowX
arrayX(i) = Cells(i, 24)
Next i
i = 1
j = 1
For i = 1 To LrowX
For j = 1 To LrowU
If arrayX(i) = arrayU(j) Then bed_in_use = bed_in_use + 1
Next j
Next i
MsgBox (bed_in_use)
End Sub

if i declare 5 values in 25 size of an array, how can i find used size of the array in VB Script?

dim a(100)
a(0)=9,a(1)=3,a(2)=-3,a(3)=8,a(4)=2
how can i find size of used array(i.e used size is 5
You have to count the non-empty elements:
Option Explicit
Function UsedElms(a)
UsedElms = 0
Dim i
For i = 0 To UBound(a)
If Not IsEmpty(a(i)) Then UsedElms = UsedElms + 1
Next
End Function
Dim a(5)
a(2) = 2
a(4) = 4
WScript.Echo "ub:", UBound(a), "sz:", UBound(a) + 1, "us:", UsedElms(a)
output:
cscript 23027576.vbs
ub: 5 sz: 6 us: 2
Here's a hacky one-liner that I just thought of. It essentially counts the number of empty elements by converting them to spaces and then trimming them off.
intLastIndex = UBound(a) - Len(Join(a, " ")) + Len(Trim(Join(a, " ")))
Just for fun! Don't go putting it into your production code. It would certainly be more efficient as a two-liner:
s = Join(a, " ")
intLastIndex = UBound(a) - Len(s) + Len(Trim(s))
Ekkehard has the proper answer here, though. This hack only works if your array is filled contiguously.

Cant understand a syntax in vbscript

I know its a really stupid question, but can anyone help me understanding these lines:
dim dmnth = array()
Redim dmnth(Rs_emp_count + 1, Rs_proj_count + 1, total_days + 1)
dmnth(emp_cnt, prj_cnt, 0) = pname & " (" & pid & ")"
thanks in advance.
dim dmnth = array()
This is an invalid statement. In VBScript you can't declare a variable and initialize it with a value in a single statement. It probably should be
Dim dmnth
dmnth = Array()
or shorter
Dim dmnth : dmnth = Array()
which declares a variable dmnth and initializes the variable with an empty array. This is, however, only useful when you want to create an empty 1-dimensional array that will grow dynamically while preserving its values (which could also be achieved via ReDim arr(-1)). Since your array has 3 dimensions, this initialization is pointless. The command below would suffice for both declaration and initialization.
Redim dmnth(Rs_emp_count + 1, Rs_proj_count + 1, total_days + 1)
If dmnth already has been declared, this resizes the variable to a 3-dimensional array with the upper bounds Rs_emp_count + 1 in the 1st dimension, Rs_proj_count + 1 in the 2nd dimension, and total_days + 1 in the 3rd dimension. Any content of the array will be discarded. If dmnth hasn't been declared, it's declared and initialized as a 3-dimensional array with the given upper bounds.
dmnth(emp_cnt, prj_cnt, 0) = pname & " (" & pid & ")"
This puts a string constructed from pname and pid into the array field at position emp_cnt, prj_cnt, 0.
dim dmnth = array(); this sets dmth to the return value of the function array(); whatever that is. It is an unnecessary step in your program; unless array() does something useful aside from returning a value back.
Redim dmnth(Rs_emp_count + 1, Rs_proj_count + 1, total_days + 1); this trashes the contents of dmth; replacing dmnth with a 3D array with sizes, for each dimension respectively, Rs_emp_count + 1, Rs_proj_count + 1 and total_days + 1. All elements are cleared.
dmnth(emp_cnt, prj_cnt, 0) = pname & " (" & pid & ")" assigns pname & " (" & pid & ")" to the (emp_cnt, prj_cnt, 0) element of the array. Remember that arrays in VBScript are zero based; i.e. (0, 0, 0) is a valid element.
First the dmnth is declared to be an array.
Then it is redeclared as a 3 dimensional array.
Then the Emp_cnt, Prj_Cnt, 0th entry is set to be pname & " (" & pid & ")"

Resources