VBScipt Array Comparison - arrays

i have a problem with VBScript.
I am trying to loop through an array to compare all the values match.
I.e i have a tring array like the one below. i want to compare each of the values match using vbscript.
tmp(0) = "12345"
tmp(1) = "12345"
tmp(2) = "12345"
tmp(3) = "12345"
tmp(4) = "12345"
If i loop over the array i will have to do this twice in order to compare the vals. But how can i handle the first values. If the first value is wrong then its never picked up as both arrays are identical. I do not know how to get around this problem. Could someone please advise.
for x=0 to UBound(tmp)
for each val in tmp
if ( tmp(x) <> val)
print (mismatch)
End if
Next
Next

Not sure if I understand your question correctly. Do you want to check if all values of an array are equal? If so, something like this should do:
elementsEqual = True
For i = 1 To UBound(tmp)
If tmp(i) <> tmp(0) Then
elementsEqual = False
Exit For
End If
Next
You don't need to compare each element with each other element to check if all are equal. If not all elements are equal, then one of them will be inequal to the first element, so you just need a single loop.

Related

Manipulating a 'dynamic' array in javascript

As part of my nightwatchjs testing script, I have a 'dynamic' array that I would like to add a character to.
So, at the moment my array has a value of 4000, but I would like this to read 4,000.
This means that I need to add a , to my array.
The issue I have however, is that this value could change the next time I run the test script, so it could be 10000 or 100000.
So I suppose what I'm asking is whether it's possible to "select a value 3 elements from the end of my array?"
So no matter what or how many elements are in the array, the array will read xx,000.
Any help would be much appreciated. Many thanks.
Does this array have a single value? If so, why is it an array instead of just a variable?
You can use toLocaleString() to add commas to a numeric value, but it will return a string and not a number.
let arr = [4000]
let num = arr[0]
let commas = num.toLocaleString()
console.log(commas)
// "4,000"

Checking value of an index in an If statement Ruby

Very new to Ruby and I am using an If statement to see if the value of an index is equal to an specific integer. I am given a string "12345" and want to see if the first char is equal to 1 so then I can delete it and post the array as a string again.
I convert the string to an array then check the value of the index[0]. I feel I am missing something very simple. Can anyone help?
def number
num = ("12345")
numArr = num.split("")
if numArr.values_at(0) == 1
numArr.shift
numArr.join("")
end
end
you can just do this
a.slice(1, a.length) if a[0] == "1"
your code would work too if you add quotes around 1 in your if condition
You can just work with the string directly no need to use split:
num = ("12345")
if num[0] == '1'
num.slice!(0)
end
puts num
Output:
"2345"

Subscript out of range when trying to get last item of an Array

For example, I have a string A-456-BC-123;DEF-456;GHI-789. And I need to search for second part:DEF-456 with keyword 456. The potential problem here is that the first part A-456-BC-123 also has the keyword 456. Currently my logic is that, split the string first using ;, split each of it again using -, get the last item of this Array, search keyword 456. Another thing is that I don't want to do a full keyword match like DEF-456, I only want to use 456 as keyword to locate DEF-456, in other words, 456 should be the last segment of the string I want.
Here are my codes:
FirstSplit= split("A-456-BC-123;DEF-456;GHI-789",";")
For each code in FirstSplit
SecondSplit = split(FirstSplit,"-")
'get Array Count
Count = Ubound(SecondSplit)
'get the last item in Array
If SecondSplit(Count-1) = "456" Then
'doing something
End if
Next
Currently, an error will generate at SecondSplit(Count-1), saying that "Subscript out of range: '[number: -1]'"
Could someone tell me how to fix it?
The real problem is here:
SecondSplit = Split(FirstSplit, "-")
You should be splitting your element which you've stored in variable code from your For Each loop. By trying to split an array you should be getting a Type Mismatch error, but perhaps vbscript is forgiving enough to attempt and return a 0 element array back or something. Anyway:
SecondSplit = Split(Code, "-")
Also, to look at the last element just use:
If secondSplit(Ubound(SecondSplit)) = "456" Then
Subtracting 1 from the ubound would get you the second to last element.
You don't need to split it twice - what you're looking for is Right():
FirstSplit = split("A-456-BC-123;DEF-456;GHI-789",";")
For each code in FirstSplit
If Right(code, 3) = "456" Then
' do something
End If
Next
Though this would also match an entry like ABC-1456. If the - symbol is a required delimiter, you'd have to say If Right(code, 4) = "-456".

How do I reassign even and odd indices of a character array into a new smaller character array in Matlab?

In matlab I have a 32x1 character array A such that
A = {'F1' 'F2' 'F3' 'F4' 'F5' 'F6' ... 'F32'};
A = A';
Now I am trying to do the following with A.
For every even index of A meaning
A{2}, A{4}, A{6}...
I want to assign those values to a 16x1 character array B and for the odd indices of A I want to assign those values to a different 16x1 array C.
I use the following code:
for i=1:32
if mod(i,2)==0
B{i} = A{i};
else
C{i} = A{i};
end
end
and it works, but only partially because it assigns the right values at for e.g. B{2} and B{4} but the values in B{1} and B{3} are the same as in B{2} and B{4}.
Can anybody tell me how to reassign even and odd indices of a character array into a new smaller character array? My problem is that I am going from a 32x1 into a 16x1 and I'm not sure how to avoid the extra 16 entries.
Many thanks!
To get this question actual answered, use the idea of Luis Mendo in the comments. You can combine it with deal to save one line of code:
[B, C] = deal(A(2:2:end), A(1:2:end))
To make your loop work, you need a second running index jj:
A = {'F1' 'F2' 'F3' 'F4' 'F5' 'F6'};
for ii = 1:6
jj = ceil(ii/2);
if mod(ii,2)==0
B{jj} = A{ii};
else
C{jj} = A{ii};
end
end

Is there a more efficient way of choosing an element from 1 array and to every element of the same array

I want to, for every element of an array ZAbs, compare it for equality to every element of the array itself and put them into another distinct array. I want the distinct array's elements to have the same index as the ZAbs array.
I did this by creating 4 nested for loops:
for pAbs2 = 1:400
for qAbs2 = 1:300
zAbsCompare = ZAbs(qAbs2, pAbs2);
for pAbs3 = 1:400
for qAbs3 = 1:300
zAbsCompare2 = ZAbs(qAbs3, pAbs3);
if (zAbsCompare == zAbsCompare2)
InitialZModEqualsImag(pAbs2,qAbs2) = InitialZImag(qAbs2, pAbs2);
InitialZModEqualsReal(pAbs2,qAbs2) = InitialZReal(qAbs2, pAbs2);
end
end
end
end
end
However, this runs really slowly. I can't think of a better way to do this, but as I'm inexperienced with MATLAB there's probably something I'm overlooking here. Any help?
EDIT: Fixed an error and restated the question.
You can do the comparison (not sure that's what you want) efficently with bsxfun:
comp = bsxfun(#eq, X, shiftdim(X,-2));
The result comp(m,n,p,q) is 1 if X(m,n) == X(p,q), and 0 otherwise.

Resources