Unable to create Nested while-loop - while-loop

I am supposed to "Write a program that uses two nested while loops to print off the rows and columns of a 3x3 grid (numbered 1 to 3), excluding the cells along the diagonal (i.e., where the row and column have the same value)."
I tried printing row, col = 1, 1 by adding one to each iteration.
row, col = 1, 1
while row != 3 and col != 3:
row += 1
col += 1
print (row, col)
Result should look like this:
1 2
1 3
2 1
vertically 12 on top, 13 in middle and 21 in the bottom.

A nested while loop generally means a second inside the first. I don't know what language you're using, but you should consider something more like:
while row <= 3:
while col <= 3:
print (row, col)
col++
col = 1
row++
This doesn't do the diagonal checks, but it demonstrates the nested loop idea:
The outer loop begins with row = 1 and col = 1.
The inner loop begins.
The inner loop counts columns 1-3, then exits;
The outer loop resets col to 1 and increments row
The next iteration of the outer loop begins with the new row.
Steps 2-5 repeat until until row = 4, at which point the outer loop exits and you're done.
Here's an example of this sort of thing in javascript, including the diagonal check:
let row = 1;
let col = 1;
// do this block until row > 3
while (row <= 3) {
// declare a new array to collect this row's output
let output = [];
// do this block until col > 3
while (col <= 3) {
// add the column or '-' to the end of the array
output.push( row === col ? '-' : col );
// increment the column
col++;
}
// row finished. emit the collected
// row with a space between the numbers
console.log(output.join(' '));
// reset column to 1
col = 1;
// do the next row
row++;
}

Related

Iterate over an xpath (table row) python

I have xpaths as follow:
/html/body/div[1]/table[3]/tbody/tr[1]/td[3]/a
/html/body/div[1]/table[3]/tbody/tr[2]/td[3]/a
/html/body/div[1]/table[3]/tbody/tr[3]/td[3]/a
/html/body/div[1]/table[3]/tbody/tr[4]/td[3]/a
As you can see, the tr[] values are changing. I want iterate over these values.
Below is the code I have used
search_input = driver.find_elements_by_xpath('/html/body/div[1]/table[3]/tbody/tr[3]/td[3]/a')
Please let me know How can I iterate over them.
This may not be the exact solution you are looking for but this is the idea.
tableRows = driver.find_elements_by_xpath("/html/body/div[1]/table[3]/tbody/tr")
for e in tableRows:
e.find_element_by_xpath(".//td[3]/a")
if you want all third td for every row, use this:
search_input = driver.find_elements_by_xpath('/html/body/div[1]/table[3]/tbody/tr/td[3]/a')
If you want only the first 3 rows use this:
search_input = driver.find_elements_by_xpath('/html/body/div[1]/table[3]/tbody/tr[position() < 4]/td[3]/a')
For looping through tds look I.e. this answer
Another alternative, assuming 4 elements:
for elem in range(1,5):
element = f"/html/body/div[1]/table[3]/tbody/tr[{elem}]/td[3]/a"
#e = driver.find_element_by_xpath(element)
#e.click()
print(element)
Prints:
/html/body/div[1]/table[3]/tbody/tr[1]/td[3]/a
/html/body/div[1]/table[3]/tbody/tr[2]/td[3]/a
/html/body/div[1]/table[3]/tbody/tr[3]/td[3]/a
/html/body/div[1]/table[3]/tbody/tr[4]/td[3]/a
You could do whatever you wanted with the elements in the loop, I just printed to show the value
Option 1: Fixed Column number like 3 needs to be be iterated:
rows = len(self.driver.find_elements_by_xpath("/html/body/div[1]/table[3]/tbody/tr"))
for row in range(1, (rows + 1)):
local_xpath = ""/html/body/div[1]/table[3]/tbody/tr[" + str(row) + "]/td[3]"
# do something with element
# cell_text = self.driver.find_element_by_xpath(local_xpath ).text
Option 2: Both Row and Col needs to be iterated:
rows = len(self.driver.find_elements_by_xpath("/html/body/div[1]/table[3]/tbody/tr"))
columns = len(self.driver.find_elements_by_xpath("/html/body/div[1]/table[3]/tbody/tr/td"))
for row in range(1, (rows + 1)):
for column in range(1, (columns + 1)):
local_xpath = ""/html/body/div[1]/table[3]/tbody/tr[" + str(row) + "]/td[" + str(column) + "]"
# do something with element
# cell_text = self.driver.find_element_by_xpath(local_xpath ).text

VLookup analogue in VBA, printing two columns based on StrComp with common first column

I'm updating a VBA script and trying to match a 4-digit code with a table and printing the two corresponding columns into my original sheet, plus handling codes missing from the reference table.
jobcodes = sample codes to match.
codematch = reference table, 1st column is reference codes, I want the corresponding values in columns 2 and 3 in K and L of "jobcodes".
At the minute I'm getting blank values in the first two rows, then #N/A errors in the rest of the sample table.
finrow3 = jobs.Cells(Rows.Count, 1).End(xlUp).Row
jobcodes = jobs.Range(("J2"), ("L" & finrow3)).Value
codematch = stat.Range("I2:K143").Value
For i = 1 To finrow3 - 1
For j = 1 To UBound(codematch, 1)
If StrComp(jobcodes(i, 1), codematch(j, 1)) = 0 Then
resulta(z, 1) = codematch(j, 2)
resulta(z, 2) = codematch(j, 3)
Else
resulta(z, 1) = ""
resulta(z, 2) = ""
End If
Next j
Next i
jobs.Range(("K2"), ("L" & finrow3)).Value = Application.Transpose(resulta)

Dynamic variable assignment based on vector element values in Octave

I need to dynamically assign variable names to each row in a matrix based on the element values of the row so that I can later reference the appropriate row based on the variable rather than an index value.
I am having trouble identifying how to store the variable name along with its associated vector so that the vector can be later pulled up by simply typing in the variable name.
So far, using a simplified example I have:
A = [1 1 0; 1 0 1; 0 1 1];
s = cell(rows(A),1)
i = 1;
for i = 1:rows(A)
if (A(i,1) == 1 & A(i,2) == 1 & A(i,3) == 0) s(i,1) = struct("x1_y1_z0",{A(i,:)})
elseif (A(i,1) == 1 & A(i,2) == 0 & A(i,3) == 1) s(i,1) = struct("x1_y0_z1",{A(i,:)})
elseif (A(i,1) == 0 & A(i,2) == 1 & A(i,3) == 1) s(i,1) = struct("x0_y1_z1",{A(i,:)});
endif
i++
endfor
However, the resulting structue s lists x1_y1_z0 = 1 1 0 in a cell [1,1] and s.x1_y1_z0 does not return [1 1 0] as I would like.
Thanks very much for your guidance towards a working solution.

Excel ActiveX textbox - count characters or change case

Two days of continual failure. I am using a barcode system which has a barcode scanner which scans a barcode of alpha-numeric text and places it into an ActiveX textbox. It enters the text one letter at a time, and upon the completion of the entire barcode, it matches up to a Case selection, which then deletes the text in the box to get ready for the next scan.
The issue I happen to be facing is inside of the textbox. For whatever reason, the text goes into the textbox and occasionally ~ (1 time in one hour or 0 times in 8 hours) it will not complete the case. The exact text inside of the textbox which matches one of the cases is not counted and stays inside the box. At this point, any future scans are appended to the end of the text inside of the box.
Below is a sample of the variables, a case, and one of the events occuring based on case selection.
Variables
Private Sub TextBox1_Change()
Dim ws As Worksheet, v, n, t, b, c, e, f, h, j, k, i1, i2, i3, i4
Set ws = Worksheets("Sheet1")
v = TextBox1.Value
n = 0
t = 0
b = 0
c = 0
e = 0
f = 0
h = 0
j = 0
k = 0
i1 = 0
i2 = 0
i3 = 0
i4 = 0
Case
Select Case v
Case "2 in x 16 ft R -1": n = 9
t = 1
b = 10
c = 1
e = 11
f = 6
g = "2 in x 16 ft"
h = 40
j = 0.296
k = 1
Stuff that is done based on case type
'n = Sets the column reference for waste - not used?
't = Sets the cutting station column to be used (1,2,3) for the sq yards, row, and column of last scanned item for each station
'b = Sets the row reference for adding cut rolls waste + regular row reference for cut rolls
'c = Sets the column reference for adding cut rolls waste + regular column refernce for cut rolls
'e = Sets the column reference for taking 1 master roll out
'f = Sets the row reference for taking 1 master roll out
'g = name of the item being used for the time stamp
'h = Number of rolls coming out of the master roll
'j = The amount of Sq yards in the cut roll (to be used for waste)
'k = Case Selection
'i1 = Count for Cutting Station 1 timestamp, row reference
'i2 = Count for Cutting Station 2 timestamp, row reference
'i3 = Count for Cutting Station 3 timestamp, row reference
'i4 = Count for Cutting Station 1 timestamp, row reference - not used in this worksheet
If k = 1 And t = 1 Then
'Cutter 1 items
ws.Cells(1, t) = b
ws.Cells(2, t) = c
ws.Cells(3, t) = j
ws.Cells(4, t) = b
ws.Cells(5, t) = c
ws.Cells(6, t) = f
ws.Cells(7, t) = h
ws.Cells(b, c) = ws.Cells(b, c) + h
' adding different number based on case
ws.Cells(f, e) = ws.Cells(f, e) - 1
' always subtracts 1 from certain range based on case
i1 = ws.Cells(1, 30)
Cells(i1, 19).Value = Format(Now, "mm/dd/yyyy AM/PM h:mm:ss")
Cells(i1, 20).Value = g
TextBox1.Activate
TextBox1.Value = ""
Remember the text enters in one character at a time until the entire barcodes information is passed into the ActiveX textbox.
I can set a max length, but upon the max length it stays in the textbox. If I set the textbox to "", the next character in the barcode starts again and the append issue continues.
Is there a way to not have the case selection start upon the entry of each single character? Is there a way to have the textbox delete the extra information. If you set it to delete something which does not match a case, then it will delete anything entered since it puts one character in at a time.
Best regards,
Ford

Looping through a specified array?

I am trying to match the row height of specific rows from one sheet to another, This works if I just remove all lines with rowlist and do For i = 1 to 200, but this takes too long. I only want to match a few row heights and not go through all between 1 and 200. My code is below:
Dim y As Double
Dim i As Long
Dim rowlist() As Variant
rowlist = Array(3, 5, 23, 30)
For i = LBound(rowlist) To UBound(rowlist)
y = Worksheets("Development").Rows(i).RowHeight
Worksheets("Final").Rows(i).RowHeight = y
Next i
When you're setting and using y, use .Rows(rowlist(i)) rather than .Rows(i).
i simply stores the index of the array, not the value, i.e.
i = 0 ; rowlist(i) =3
i = 1 ; rowlist(i)= 5
i = 2 ; rowlist(i)= 23
i = 3 ; rowlist(i)= 30
So you're correct in looping from LBound(rowlist) to UBound(rowlist), you just need to make sure that you're using the values stored in the array within that loop.