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 & ")"
Related
I have written code for an array of numbers which prints out. I'm now writing code to split the array into even and off numbers. I've started off with an if statement to separate the numbers but i'm struggling to find a solution on how to do it. My code below is failing as it's unable to split the numbers.
Sub main()
a=Array(5,10,15,20)
for each x in a
Msgbox(x)
If MyArray(I) / 2 = MyArray(I)
List1.AddItem MyArray(I) ' Even Integers
Else
List2.AddItem MyArray(I) ' Odd Integers
End if
next
End Sub
As Lankymart suggests, the simplest approach would be to use Mod() and check if the remainder is 1 or 0, but you can also do it with the approach you seemed to be working towards:
If MyArray(index)/2 = Int(MyArray(index)/2) Then
' Even number
Else
' Odd number
End If
Mod() approach:
If MyArray(index) Mod 2 = 0 Then
' Even number
Else
' Odd number
End If
Here's a complete subroutine that demonstrates what you are trying to do:
Dim arr(4) As Integer
Dim arrEven() As Integer
Dim iEvenValues As Integer
Dim arrOdd() As Integer
Dim iOddValues As Integer
Dim iCounter As Integer
' Initialize array
arr(0) = 5
arr(1) = 10
arr(2) = 15
arr(3) = 20
For iCounter = 1 To UBound(arr)
If arr(iCounter - 1) Mod 2 = 0 Then
iEvenValues = iEvenValues + 1
ReDim Preserve arrEven(iEvenValues)
arrEven(iEvenValues - 1) = arr(iCounter - 1)
Else
iOddValues = iOddValues + 1
ReDim Preserve arrOdd(iOddValues)
arrOdd(iOddValues - 1) = arr(iCounter - 1)
End If
Next
Dim sValues As String
sValues = "Even values (" & iEvenValues & "):"
For iCounter = 1 To UBound(arrEven)
sValues = sValues & " " & arrEven(iCounter - 1)
Next
MsgBox sValues
sValues = "Odd values (" & iOddValues & "):"
For iCounter = 1 To UBound(arrOdd)
sValues = sValues & " " & arrOdd(iCounter - 1)
Next
MsgBox sValues
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
I'm need to be able to have a 2d array, where the length of second array varies on a case by case basis. To this end, I made an array that contains other arrays with the following code:
Dim timeline
ReDim timeline(days)
for reDate = beginDate to endDate
timeline(DateDiff("d", beginDate, reDate)) = Array(0)
next
The problem I am having is that I am unable to change the size of any of the arrays contained by timeline, as ReDim doesn't seem to work when I do this. Does anyone know how I would go about this?
Try the below snippet, using a as temporary array variable:
Dim timeline, a
ReDim timeline(days)
' initial fill the array
For reDate = beginDate to endDate
a = Array()
ReDim a(99)
timeline(DateDiff("d", beginDate, reDate)) = a
Next
' redim sub arrays
For reDate = beginDate to endDate
' assign subarray to a
a = timeline(DateDiff("d", beginDate, reDate))
' redim a
ReDim Preserve a(199)
' put changed array into root array
timeline(DateDiff("d", beginDate, reDate)) = a
Next
In this case each subarray contains 100 elements first, and 200 after redim.
"Does not work" and error messages ("Object required") without code/context are not the best way to ask a question. The first is a complete waste of time; the second may indicate that you used Set where you shouldn't (VBScript Arrays are not objects, so there shouldn't be any Set in the code).
This demonstrates the same facts that #omegastripes pointed out, but gives hints wrt possible pitfalls:
Option Explicit
Dim AoA : AoA = Split("s o m e|w o r d s|o f|d i f f e r e n t|l e n g t h", "|")
Dim i
For i = 0 To UBound(AoA)
AoA(i) = Split(AoA(i))
Next
WScript.Echo "----- test data:"
For i = 0 To UBound(AoA)
WScript.Echo Join(AoA(i), "")
Next
WScript.Echo "----- array assignment copies (For Each elem ... does not 'work'):"
Dim e
For Each e In AoA
e = "zap"
Next
For Each e In AoA
WScript.Echo Join(e, "")
Next
WScript.Echo "----- array assignment copies (change needs index access):"
For i = 0 To UBound(AoA)
e = AoA(i)
e(0) = UCase(e(0)) ' just changes the copy (e)
WScript.Echo Join(e, "")
AoA(i)(1) = UCase(AoA(i)(1)) ' access via indices changes org collection
WScript.Echo Join(AoA(i), "")
Next
WScript.Echo "----- replace whole subarrays (re-dimensioned dynamically):"
Dim n, m
For i = 0 To UBound(AoA)
e = AoA(i)
n = UBound(e)
ReDim Preserve e(n * 2 + 1)
For m = n + 1 To UBound(e)
e(m) = UCase(e(m - n - 1))
Next
AoA(i) = e ' replace whole element
WScript.Echo Join(AoA(i), "")
Next
output:
cscript 37951664.vbs
----- test data:
some
words
of
different
length
----- array assignment copies (For Each elem ... does not 'work'):
some
words
of
different
length
----- array assignment copies:
Some
sOme
Words
wOrds
Of
oF
Different
dIfferent
Length
lEngth
----- replace whole subarrays:
sOmeSOME
wOrdsWORDS
oFOF
dIfferentDIFFERENT
lEngthLENGTH
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.
I need to create multi-dimensional array of strings. Each row of the array can have varying number of strings. Something like the follwing code:
twoDimension = Array(Array())
ReDim Preserve twoDimension(3)
For i = 0 to 2
If i = 1 Then
twoDimension(i) = Array(1,2,3)
End If
If i = 2Then
twoDimension(i) = Array(1,2,3,4,5)
End If
Next
How about a dictionary
Set a = CreateObject("Scripting.Dictionary")
a.Add 0, Array(1,2,3)
a.Add 1, Array(4,5,6)
MsgBox a.Count
MsgBox a.Item(0)(2)
MsgBox a.Item(1)(1)
There's nothing wrong with having jagged arrays in VBScript. There are some minor issues with your code (ReDim to 3 but only assigning values to 2, unnecessarily using a For loop to assign values), but in general, that's the correct syntax to use.
Option Explicit
Dim twoDimension, i, j
twoDimension = Array(Array())
ReDim Preserve twoDimension(2)
twoDimension(1) = Array(1,2,3)
twoDimension(2) = Array(1,2,3,4,5)
For i = 0 To UBound(twoDimension)
For j = 0 To UBound(twoDimension(i))
WScript.Echo "(" & i & "," & j & ") = " & twoDimension(i)(j)
Next
Next