if variable part changes in vbscript - vbscript

Is it possible to have a variable where only a part of it is changing?
I have a scale that gives me a value 6 times a second. Every time the weight on the scale changes, it gives me a value like $r00000 or 4$00400. But if the value stays constant for 1 sec it returns $b01234.
My problem is that I can't find a way to use the "$b" in an if statement, so that after $b I have a value that changes.
Dim MyString, MyArray, i
Dim test
MyString = Scale_Value
If MyString="*$b**????***" Then
MyArray = Split(MyString, "$b", -1, 1)
For i = 0 To UBound(MyArray)
test = (MyArray(i))
tmpPWgt = (CStr(Mid(test, 1,1) & "." & Mid(test, 2)))
Next
Else
tmpPWgt="0.000"
End If

As you seem to be interested in the $b values only, use Left() and Mid() to get the parts for each input:
>> a = Split("$r00000 $b01234 4$00400 $b54321 $r55555")
>> for each m in a
>> WScript.Echo "----", m
>> if "$b" = Left(m, 2) Then
>> m = CLng(Mid(m, 3))
>> WScript.Echo "1 sec w:", m
>> end if
>> next
>>
---- $r00000
---- $b01234
1 sec w: 1234
---- 4$00400
---- $b54321
1 sec w: 54321
---- $r55555
>>

...
If Left(MyString, 2) = "$b" Then
...

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.

How to capitalise a random letter?

In VBScript is it possible to select a random letter and capitalize it until all letters have been capitalized at least once?
Dim a
a = "Hello"
For i=o To Len(a)-1
If Mid(a,i+1, 1) = Mid(a, i+1, 1) Then
b = Mid(a, i+1, 1)
MsgBox b
End If
Next
That's some code I gathered. So far it reads a string letter by letter.
I want the output to be something like:
hello
Hello
hEllo
etc.
but I can't see how to do it without getting into super complex Mid Left Right statements that become confusing. Is it possible? Or do I need to use something like Mid(LCase(s,1,1) & Mid(UCase(s,2,1)) & Mid(LCase(s,3,3)?
Use Len() to determine the positions of letters to capitalize and Left() + UCase(Mid()) + Mid() to actually uppercase the letter at p =
Option Explicit
Dim s : s = "hello"
Dim l : l = Len(s)
Dim i : i = 0
Do Until s = UCase(s)
Dim p : p = Fix(Rnd() * l) + 1
If Mid(s, p, 1) <> UCase(Mid(s, p, 1)) Then s = Left(s, p - 1) & UCase(Mid(s, p, 1)) & Mid(s, p + 1)
WScript.Echo i, p, s
i = i + 1
Loop
output:
cscript 52911013.vbs
0 4 helLo
1 3 heLLo
2 3 heLLo
3 2 hELLo
4 2 hELLo
5 4 hELLo
6 1 HELLo
7 4 HELLo
8 5 HELLO

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 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.

Remove duplicates from an array

How can I remove duplicates from an array in vbscript?
Code:
dim XObj(100),xObjXml
for s=0 to xObjXml.length-1
XObj(s)=xObjXml(s).getAttribute("xsx")
next
Please suggest a better answer for this.
Use a Dictionary to gather the unique items of the array:
>> a = Array(1, 2, 3, 1, 2, 3)
>> WScript.Echo Join(a)
>> Set d = CreateObject("Scripting.Dictionary")
>> For i = 0 To UBound(a)
>> d(a(i)) = d(a(i)) + 1
>> Next
>> WScript.Echo Join(d.Keys())
>>
1 2 3 1 2 3
1 2 3
>>
(BTW: There is no .length property for VBScript arrays)
Added:
The .Keys() method of the dictionary returns an array of the (unique) keys:
>> b = d.Keys()
>> WScript.Echo Join(b), "or:", b(2), b(1), b(0)
>>
1 2 3 or: 3 2 1
Added II: (aircode!)
Trying to get the unique attributes of the objects in an XML collection:
Dim xObjXml : Set xObjXml = ... get some collection of XML objects ...
Dim dicAttrs : Set dicAttrs = CreateObject("Scripting.Dictionary")
Dim i
For i = 0 To xObjXml.length - 1
Dim a : a = xObjXml(i).getAttribute("xsx")
dicAttrs(a) = dicAttrs(a) + 1
Next
Dim aAttrs : aAttrs = dicAttrs.Keys()
Added III (sorry!):
.Keys() is a method, so it should be called as such:
Dim aAttrs : aAttrs = dicAttrs.Keys()
Added IV:
For a working sample see here.
If you don't want a Dictionary you can use the following to compare each element in the array to itself.
Info = Array("Arup","John","Mike","John","Lisa","Arup")
x = 0
z = ubound(Info)
Do
x = x + 1
Do
z = z - 1
If x = z Then
Info(x) = Info(z)
ElseIf Info(x) = Info(z) Then
Info(x) = ""
End If
Loop Until z=0
z = ubound(Info)
Loop Until x = ubound(Info)
For each x in Info
If x <> "" Then
Unique = Unique & Chr(13) & x
End If
Next
MsgBox Unique

Resources