I'm trying to create code that would add variables to list. Then it would split all of list elements by :, returning only the second part. In the end all returned strings would be trimmed.
To give you better understanding let me show you an example:
Add "a : b " and " c : d " to the list.
Split elements so only "b " and "d " are returned.
Trim "b " and "d ".
So far I have came up with something like this but it doesn't work and I have no idea why.
Set list = CreateObject("System.Collections.ArrayList")
list.Add "bob : kid"
list.Add "ryan : alex"
list.Add "kate : andy"
list.Add "alice : mandy"
For Each item In list
Dim item As String
Dim words As String() = item.Split(New Char() {":"c})
For Each word In words
WScript.Echo word
Next
Next
Your code doesn't work because it's not valid VBScript. You have several issues:
VBScript doesn't support typed variable declarations (Dim var As Type). Use Dim var if you want to explicitly define a variable. Note, however, that in VBScript defining variables is only required when you put Option Explicit at the beginning of your script.
VBScript doesn't support combined declaration and definition of variables (Dim var = value). You need two separate statements for defining a variable and assigning a value to it. The two statements can be put in one line by separating them with a colon (Dim var : var = value), but they're still two separate statements.
In VBScript string is a primitive data type. VBScript strings are not objects with properties. For splitting a string use the Split() function.
Don't re-define your loop variable inside your loop.
Change your loop to something like this and it should do what you want:
For Each item In list
Dim words : words = Split(item, ":")
For Each word In words
WScript.Echo Trim(word)
Next
Next
If you want just the second element from each split item you'd do something like this:
Dim words : words = Split(item, ":")
WScript.Echo Trim(words(1))
or like this:
WScript.Echo Trim(Split(item, ":")(1))
Related
I'm teaching myself VBS and I decided to write a message encryption program. It uses the left and right functions in a loop to read every individual character.
DO
wscript.sleep 100
if Az=0 then
EXIT DO
end if
CR=right(message,aZ)
DEL=left(CR,1)
aZ=aZ-1
zZ=zZ+1
supra=""
supra="supra"
CALL KEYCOUNT
CD=left(keyword,zZ)
TAC=right(CD,1)
....
From there, it sets every character equal to a different letter based on an encryption keyword and moves onto the next character. My problem is I don't know how to deal with spaces in the message. Is there a way to make a variable have the value of a space? I've tried:
set var=space(1)
set var="&"" ""&"
set var=""
set var=" "
set var=""" """
I'm certain there are things I'm not thinking of
Thanks
Joseph
set statement assigns an object reference to a variable or property, or associates a procedure reference with an event. That isn't our case.
var=space(1) ' var contains one space character
var="&"" ""&" ' var contains &" "&
var="" ' var contains a string of zero length
var=" " ' var contains one space character
var=""" """ ' var contains " "
Or, if you would like, declare a constant for use in place of literal value of space (anywhere in your script) as follows:
Const vbSp=" "
Constants are public by default. Within procedures, constants are always private; their visibility can't be changed. Within a script, the default visibility of a script-level constant can be changed using the Private keyword. There are variations:
Private Const vbSp=" "
Public Const vbSp=" "
The problem is that you use Set (cf. here) when assigning a simple/non-object value to a variable. If you loose it, your experimental statements will 'work' (compile & run without error). Look at the output and you'll see that
var = " "
is the correct and most efficient way to assign a (string containing one) space to a variable.
I've written a lot of IF statements in VBScript, but haven't gone much beyond that so apologize for my lack of experience. I hope what I'm asking is simple to do.
I want to output item identifiers created by three combined recordset field values and add "B" "C" "D" etc., to any duplicates. Duplicates are rare, but do happen occasionally. I want to do this for meaningful item identification which autonumbers do not provide.
The following example works to combine fields, but then I need to include script to loop and find the duplicates and add the appropriate alpha character.
FYI: a = alpha character, b = alpha character, c = reformatted date
<% Dim idCode
a = (rs_table.Fields.Item("CodeA").Value)
b = (rs_table.Fields.Item("CodeB").Value)
c = (fixedDate(rs_table.Fields.Item("Date").Value))
idCode = (a) & (b) & (c)
Response.write idCode
%>
example output: LC032414
example dupe output: LC032414B
Thanks, I'm almost afraid to ask and may find this more pain than what it's worth!
I would probably use a Dictionary to store the ID's, since you can add each as a key (which must be unique) and test the Dictionary for its existence. Something like this:
' Early on... create a dictionary...
Set d = CreateObject("Scripting.Dictionary")
' Loop through your records...
Do Until rs_table.EOF
' Determine your ID...
idCode = rs_table("CodeA") & rs_table("CodeB") & fixedDate(rs_table("Date"))
' Check for existence in the dictionary...
If d.Exists(idCode) Then
' ID already exists. Keep testing suffixes until we find an availability...
strLetter = "B"
Do While d.Exists(idCode & strLetter)
strLetter = Chr(Asc(strLetter) + 1)
Loop
d.Add idCode & strLetter, "" ' Add the ID/key. The value is unimportant.
Else
' This ID doesn't exist yet. Just add it.
d.Add idCode, "" ' Add the ID/key. The value is unimportant.
End If
rs_table.MoveNext
Loop
When it comes time to print your ID's, you can just iterate the dictionary's Keys collection:
For Each k In d.Keys
Response.Write k
Next
I am trying to get the variable name used in for each loop printed in vbs but unable to do so.
x=2, y=3, z=7
a = array (x,y,z)
for each element in a
wscript.echo element
next
The above example prints the variable element value (2/3/7) rather than element name (x/y/z). how do you i get to print the element name.
Please Help!
Did you consider using a dictionary?
Dim dict : Set dict = CreateObject("Scripting.Dictionary")
dict.item("x") = 2
dict.item("y") = 3
dict.item("z") = 7
dim key, value
For Each key in dict.Keys
value = dict.item(key)
wscript.echo key & " = " & value
Next
You can't in normal programming. You are the programmer, you know the names. You put the names in the output (msgbox "Variable name is A " & A).
You can put your code into strings, process it how you want, then execute it with eval or execute (see Help).
You can do the above but use the script control (see Google).
But you know the names, and if you want to display them simply, then you type their names into the message strings.
x=2, y=3, z=7
a = array (x,y,z)
names = array ("x","y","z")
for each element in names
wscript.echo element
next
If you want to show each element with his value:
x=2, y=3, z=7
a = array (x,y,z)
names = array ("x","y","z")
for i=0 to 2
wscript.echo names(i) & "=" & a(i)
next
I have 6400+ records which I am looping through. For each of these: I check that the address is valid by testing it against something similar to what the Post Office uses (find address). I need to double check that the postcode I have pulled back matches.
The only problem is that the postcode may have been inputted in a number of different formats for example:
OP6 6YH
OP66YH
OP6 6YH.
If Replace(strPostcode," ","") = Replace(xmlAddress.selectSingleNode("//postcode").text," ","") Then
I want to remove all spaces from the string. If I do the Replace above, it removes the space for the first example but leave one for the third.
I know that I can remove these using a loop statement, but believe this will make the script run really slow as it will have to loop through 6400+ records to remove the spaces.
Is there another way?
I didn't realise you had to add -1 to remove all spaces
Replace(strPostcode," ","",1,-1)
Personally I've just done a loop like this:
Dim sLast
Do
sLast = strPostcode
strPostcode = Replace(strPostcode, " ", "")
If sLast = strPostcode Then Exit Do
Loop
However you may want to use a regular expression replace instead:
Dim re : Set re = New RegExp
re.Global = True
re.Pattern = " +" ' Match one or more spaces
WScript.Echo re.Replace("OP6 6YH.", "")
WScript.Echo re.Replace("OP6 6YH.", "")
WScript.Echo re.Replace("O P 6 6 Y H.", "")
Set re = Nothing
The output of the latter is:
D:\Development>cscript replace.vbs
OP66YH.
OP66YH.
OP66YH.
D:\Development>
This is the syntax Replace(expression, find, replacewith[, start[, count[, compare]]])
it will default to -1 for count and 1 for start. May be some dll is corrupt changing the defaults of Replace function.
String.Join("", YourString.Split({" "}, StringSplitOptions.RemoveEmptyEntries))
Because you get all strings without spaces and you join them with separator "".
I am trying to replace the double quotes in a string with a single quote, got the following code but get error message saying "Object Required strLocation"
Sub UpdateAdvancedDecisions(strLocation)
Dim d
Dim strLLength
strLLength = Len(strLocation) - 1
For d = 0 To strLLength
alert strLocation
strValue = strLocation.Substring(2,3)
If strLocation.substring(d,d+1)=" " " Then
strLLength = strLLength.substring(0, d) + "'" + strLLength.substring(d + 1,strLLength.length)
Next
End Sub
As Helen said, you want to use Replace, but her example assigned the result to your weird strLLength variable. Try this instead:
strLocation = Replace(strLocation, """", "'")
This one line does the job you asked about and avoids all the code currently in your given subroutine.
Other things that are problems in the code you posted:
a variable holding a number like the length of a string would not have a "str" prefix, so strLLength is misleading
strings in VBScript are indexed from 1 through length, not 0 through length-1
there is no "alert" keyword in VBScript
you assign a value to strValue, then never use it again
you need to use Mid to get a substring, there is no "substring" string method in VBScript
c = Mid(strLocation, d, 1) ' gets one character at position d
The more I look at this, the more clear it is that its some JavaScript that you're trying to run as VBScript but are not translating at all correctly.
Use a reference for VBScript like one of the following:
MSDN Library: VBScript Language Reference
W3Schools VBScript Tutorial