I am attempting to use a repeat-while loop in Swift. I am trying to use it to grab the items within an array and add it to a variable.
var finalEquation = ""
var arrayPoint = 0
repeat {
arrayPoint += 1
finalEquation = finalEquation + String(equation[arrayPoint])
} while (arrayPoint < equation.count)
So I have a variable called "arrayPoint", this increments by 1 each time the loop runs. So the problem I'm having I'm having is with the line:
finalEquation = finalEquation + String(equation[arrayPoint])
This line takes the item in the array, which is equal to arrayPoint and adds it that array item to a variable.
When ever I build and run the code, it compiles without any errors and does not stop me. Then whenever the loop gets activated, the application freezes, and in the output window, I get something saying.
fatal error: Index out of range
(lldb)
Then on that line of code that I mentioned earlier, it says: Thread 1: EXC_BAD_INSTRUCTION
First of all you might run this loop in UI thread which is wrong.
2nd you are increment the variable before fetch the value. Please move arrayPoint+=1 just after finalEquation line.
Putting the incrementing before your usage of the arrayPoint makes the range [1, count], but the array only has indices [0, count - 1].
You should move the increment to be the last thing in the repeat block.
var finalEquation = ""
var arrayPoint = 0
repeat {
finalEquation = finalEquation + String(equation[arrayPoint])
arrayPoint += 1
} while (arrayPoint < equation.count)
On a side note, arrayIndex would be a more appropriate name. The number used to reference an element in an array is called an "index".
Also, you should read about breakpoints, so that you can better debug off-by-one errors like this yourself.
Related
I am trying to loop through an array that contains split strings, done via this line:
repHolder() = Split(rep, ",")
That's all fine and good, however, when I try to loop through this repHolder() array via a for loop, I am met each time with a subscript out of range error.
This makes no sense to me. When I step through the array it fails on the first element every time; this line:
If repHolder(j) = counter Then
I tried setting j to 0 and 1, both of which failed on the first sequence of the loop. This suggests to me because the array doesn't have a defined size; that I cannot loop through it this way, but that still makes little sense to me as it is still filled with elements.
Here is the entire code block of what I am trying to do:
Dim repHolder() As String
Dim strHolder() As String
Dim counter As Variant
Dim j As Integer
For Each rep In repNames()
repHolder() = Split(rep, ",")
Next rep
For Each rangeCell In repComboRange
k = 1
Do
If rangeCell.Value = repCombos(k) Then 'At this point, if rangecell = repcombos(k)
Range(rangeCell, rangeCell.End(xlToRight)).Copy
strHolder() = Split(rangeCell.Value, "/")
For Each counter In strHolder()
Stop
For j = 1 To 17
If repHolder(j) = counter Then
You are looping through repNames() and setting this new array via split (over and over again for each repName element...)
For Each rep In repNames()
repHolder() = Split(rep, ",")
Next rep
Every iteration of this loop resets repHolder() to a split of the rep element dropping whatever values were just set in that array in the previous iteration. So once it's done only the last element of RepNames() has been split into the repHolder() array.
For instance, if RepNames() looks like:
Element 0: "james,linda,mahesh,bob"
Element 1: "rajesh,sam,barb,carrie"
Element 2: ""
Then after all this iterating your repHolder array is going to be empty because there is nothing in the final element.
Stick a breakpoint (F9) on you For Each rangeCell In repComboRange line and look at your Locals pane in VBE. Check out the values that are stored in your repHolder() array at that point in time. I suspect there will be nothing in there.
The other oddball here is that you are looping 1 through 17. repHolder() will be a 0-based array so that should be 0 through 16. But... even that is nonsense since this really only makes sense as a For Each loop (or to use the uBound(repHolder) to determine how many times to loop:
For Each counter In strHolder()
Stop
For each repHolderElem in repHolder
If repHolderElem = counter Then
....
Next repHolderElem
Hi I'm working on a macro in VBA for excel. I have a nested for loop in my code as seen below. The second loop does not run; even a MsgBox() command within the second loop doesn't result in any action and the program just seems to skip over the nested loop with no reported errors.
In plain English this nested loop should:
1) Take the string from the i entry in the array categories_string (first for loop).
2) iterate through the 300+ rows in column "AE" in the excel file (second for loop, "length" is the length of the column of data in "AE")
3) look for string matches and add 1 to the corresponding entry in the categories_value array which is populated with variables set to 0 (the if statement).
For i = LBound(categories_string) To UBound(categories_string)
For p = 1 To p = length
If Worksheets("sheet1").Cells(p + 2, "AE").Value = categories_string(i) Then
categories_value(i) = categories_value(i) + 1
End If
Next
Next
Change
For p = 1 To p = length
to
For p = 1 To length
Should fix you right up.
You may want to consider Scott Cranner's comment as well, especially if your categories_string is large and length is large. Or, just do it for the practice
I have a couple hundred line program (including functions) in essentially free-form Fortran. At one point, I have a pair of nested do loops that call functions and store results in matrices. However, I don't believe any of that is the problem (although I could be wrong).
Immediately after the first do loop starts, I define an array using a column of another array. Immediately after that, the index is always set to 3. I haven't been able to find any useful information in the usual places. I've included a fragment of the code below.
do i = 1,n
print *, 'i:',i ! Gives i = 1
applyto = eig_vec(:,i)
print *, i ! Gives i = 3
state1 = create_state(ground,applyto,state,bin_state,num_s,ns)
first = destroy_state(ground,state1,state,bin_state,num_s,ns)
state1 = destroy_state(ground,applyto,state,bin_state,num_s,ns)
second = create_state(ground,state1,state,bin_state,num_s,
1 ns)
do j = 1,n
bra = eig_vec(:,j)
a_matrix(j,i) = sum(bra*first + bra*second)
matrix(j,i) = sum(bra*first - bra*second
end do
end do
Is this a bug? Am I missing something obvious? I am compiling the code with a high level of optimization, if that could potentially be a source of problems. I'm relatively new to Fortran, so debugging flags or commands (for gdb - I believe that's all I have available) would be welcome.
I have a matrix that looks like :
A =[
1 5
2 10
3 12
4 25
5 8]
Let's assume that I want to remove the rows that contain elements that are larger than 10. I have been trying to use a for loop and simply read the matrix and use
for ii = 1:5
for jj = 2
if A(ii,jj) > 10
A(ii,2) = [];
end
end
end
The problem is that, I keep receiving errors regarding the size of a matrix. As one row is deleted, the size of the matrix is reduced and I know that I have to set size(A)=size(A)-1 but it doesn't work. Any help is appreciated!
You can't remove a single element when using subscript notation, so your error is here:
A(i,2) = [];
You have two choices, either remove the entire row:
A(i,:) = [];
or else linearise your matrix and remove single elements (but then you won't be able to recover you original shape:
for ii = numel(A):-1:1
if A(ii) > 10
A(ii) = [];
end
end
but using a loop for this is unnecessary and probably inefficient. You can do it using logical indexing like so:
A(any(A'>10),:) = [];
to remove the whole row or else to just remove single elements try:
A(A>10) = []
but then you'll see that you end up with a row vector (linearized) result
Here is a solution, quite similar to the one of #Dan. Basically this keeps what you want instead of removing what you don't want:
A(all(A<=10,2),:)
This code simply completes a shift of characters for every negative variable in the loop. It then displays this text for every loop completed. (The name variable is actually a parameter for one of the subroutines, so does need to remain being called 'variable')
Counter = 0
dim counterarray(24)
For variable = -1 to -25
completeshift()
displaytext()
counter = counter + 1
next
So in this code, i would like to know how to step up each variable in the array every time the loop is complete. Basically i need the first
loop displaytext() to go into counterarray(0), the second to go into counterarray(1) etc until all of them have been completed.
Not entirely sure what your question is, but if you want that loop to work you need to add step - 1
For variable = -1 to -25 step -1
completeshift()
displaytext()
counter = counter + 1
next
You are not using variable for anything so you may as well write,
Dim counterarray(24)
For i = 0 to 24
completeshift()
counterarray(i) = displaytext()
Next
probably too elaborate but ...
Dim count = 25
Dim counterArray(count - 1) As String
Enumerable.Range(0, count).Zip(Enumerable.Range(-count, count).Reverse(),
Function(counter, variable) counterArray(counter) = DisplayText())