While iterating through a loop some items are getting missed out - ewam

The method that I am trying to run is as below where I try iterate through the sub contracts in a GBP and then I have an inner loop to iterate through the coverages in each sub contract.
forEach curSubscribedSubContract in theSubContracts
; Avoid building empty contract node
if subOptions.count > 0
new(ParamProduct)
new(ParamSubscribedProduct)
forEach curOption in subOptions downTo
theProduct = curOption.GetProduct
new(ListOfCoverages)
if Motor.AreSameThings(theProduct, curSubscribedSubContract.GetProduct)
ParamSubscribedProduct.InitParamFromObjectAtDate(curSubscribedSubContract,
AtDate)
ParamProduct.Product = ParamSubscribedProduct
;Set Context for PlanDesign options
AFCI_SLI.CoverageInContext = curOption
covData = self._GetCoverageData(curOption, AtDate)
;Reset coverage context
AFCI_SLI.CoverageInContext = Nil
ListOfCoverages.AppendObject(covData)
if subOptions.RemoveObject(curOption)
endIf
endIf
forEach curCoverageData in ListOfCoverages
ParamProduct.Coverages[-1] = curCoverageData
endFor
dispose(ListOfCoverages)
endFor
_Result[-1] = ParamProduct
endIf
endFor

are items missing from the subcontracts or suboptions?
I think the following line of code has something to do with it:
if subOptions.RemoveObject(curOption)
endIf
I am not sure if it is a good practice to remove objects from a list which is being iterated..
also
try removing the 'down to" from the foreach loop of suboptions

Related

If statement not working AutoIT

I am having trouble with the code I am writing. I have a table which lists staff member details. I am trying to have an email address copy to the clipboard when you select a record and right click. I have the following code:
#include <GUIConstantsEx.au3>
#include <mssql.au3>
#include <MsgBoxConstants.au3>
#include <Array.au3>
#include <WindowsConstants.au3>
#include <AutoItConstants.au3>
global $title = "E-Mail address lookup"
global $name = InputBox($title,"Please type the name of the person you wish to find")
global $sqlCon = _MSSQL_Con("server", "username", "password", "directory-plus")
global $result = _MSSQL_GetRecord($sqlCon, "autoit_view","*", "WHERE cn LIKE '%" & StringStripWS($name,3) & "%'")
if StringLen(StringStripWS($name,3)) < 1 then
MsgBox(0, $title, "Name cannot be empty")
Else
Global $rset = UBound($result) - 1
Global $ControlID = GUICreate($title, 500, 150)
Global $idListview = GUICtrlCreateListView("Deparment|E-Mail Address|Name|Telephone Number", 10, 10, 480, 150)
for $count = 1 to $rset step 1
GUICtrlCreateListViewItem($result[$count][0] & "|" & $result[$count][2] & "|" & $result[$count][1] & "|" & $result[$count][2], $idListview)
if MouseClick($MOUSE_CLICK_RIGHT)==1 Then
ClipPut($result[$count][2])
EndIf
Next
GUISetState(#SW_SHOW)
GUISetState()
While 1
Global $Msg = GUIGetMsg()
Switch $Msg
Case -3, $ControlID
Exit
EndSwitch
WEnd
EndIf
I would have thought my IF statement within the for loop would do the trick but, when I run my code, it just copies whatever is in email address column in the last row when in fact I want it to copy the email address when you right click on a particular row but I'm not sure how to do it.
Your code has two major problems. The first is that MouseClick() doesn't check for the mouse button being pressed, but instead sends a mouse button click. Since sending a mouse button click is going to succeed, MouseClick($MOUSE_CLICK_RIGHT)==1 will evaluate to true. For each list view item you create, you are putting the email address on the clipboard.
The second problem is that the if statement is in the wrong place. It's executed just after you create each list view item.
Instead, change your While statement as follows
While 1
Global $Msg = GUIGetMsg()
Switch $Msg
Case -3, $ControlID
Exit
case $GUI_EVENT_SECONDARYDOWN
$selecteditem=StringSplit(GUICtrlRead(GUICtrlRead($idListview)),"|")
if #error=0 and $selecteditem[0]>1 Then
ClipPut($selecteditem[2])
EndIf
EndSwitch
WEnd

iterate and print all date in range in twig

I am trying to iterate all dates in twig
I have tried using this:
{% for x in range(startDate|date('U'), endDate|date('U'), 86400 ) %}
{{ x|date('m/d') }}
{% endfor %}
It worked in some dates.
but if my startDate is 10/01/2015 and endDate is 10/31/2015
10/25 is displayed twice.
the scenario can be replicated on octobers of any year on 4th sundays
I don't know if it's only me that can replicate this.
Is there any other way to iterate all dates in range in twig?
The \DatePeriod class was created for exactly this purpose, and it's available since php5.3.
$start = DateTime::createFromFormat("Y-m-d", "2015-10-01");
$end = DateTime::createFromFormat("Y-m-d", "2015-11-01");
$interval = new DateInterval("P1D");
$range = new DatePeriod($start, $interval, $end);
foreach ($range as $date) {
var_dump($date->format("Y-m-d"));
}
You can try it at: https://3v4l.org/vFsb6

If _StringBetween didn't find the value

I have created a simple script in AutoIT that checks if a string exists or not, if it exists it will show label else it will show another label here is the script :
$text = $oHTTP.ResponseText
$status = $oHTTP.Status
If $status = 200 Then
$array = _StringBetween($text, '<span class="d1">', "</span>")
If $array[0] == "" Then
GUICtrlSetState($v2l1, $GUI_SHOW)
ELSE
GUICtrlSetState($v2l2, $GUI_SHOW)
EndIf
Else
ConsoleWrite(#error)
EndIf
if it find anything then the label will appear but if it didn't find the string it gives me an error and exit:
Subscript used on non-accessible variable.:
If $array[0] == "" Then
If $array^ ERROR
So is there anyway i can fix this ? I mean if $array didn't find the string it make $v2l2 visible.
Thanks in advanced.
You should use IsArray to test whether you received a valid array or not. This procedure is recommended whenever you call a function that returns an array.
If $status = 200 Then
$array = _StringBetween($text, '<span class="d1">', "</span>")
If IsArray($array) Then
; process the array
Else
; handle the error
EndIf
EndIf
Use #error which is set by _StringBetween(), this is proper AutoIt coding.
If $status = 200 Then
$array = _StringBetween($text, '<span class="d1">', "</span>")
If Not #error Then
; process the array
Else
; Handle the error
EndIf
EndIf

Autoit: Creating a Function?

I have a GUI that records the list of all the chm in a folder. When 'RUN' button is clicked, it will open the first chm in the list until so on. Then I have created a function that should expand the tree.
My problem is my function, it works until MsgBox() and stops there. When I ran my program there is no error indicated.
#include <GuiConstantsEx.au3>
#include <GuiListBox.au3>
#include <GuiTreeView.au3>
#include <File.au3>
#include <Array.au3>
;GUI
$guiTitle = "Automation"
GUICreate($guiTitle, 250, 430)
Global $hWnd = ControlGetHandle("[CLASS:HH Parent;TITLE:AutoIt Help]", "", "[CLASS:SysTreeView32; INSTANCE:1]")
Global $hChild = _GUICtrlTreeView_GetFirstChild($hWnd, 0)
Local $source = InputBox("Source Folder","Please enter the source folder","")
;InputBox
If #error = 1 Then
Exit
EndIf
If #error = 4 Then
Exit
;GUI_List
Else
$add = GUICtrlCreateButton("Show", 10, 53, 230, 20)
$picList = GUICtrlCreateList("", 10, 78, 230, 300)
$run = GUICtrlCreateButton("Run", 170, 385, 70, 30)
GUISetState(#SW_SHOW)
While 1
$msg = GUIGetMsg()
Switch $msg
;add
Case $add
Global $FileList = _FileListToArray($source, "*.chm")
If #error = 1 Then
MsgBox(0, "", "No Files Found.")
Exit
EndIf
If #error = 4 Then
MsgBox(0, "", "No Files Found.")
Exit
EndIf
For $i = 1 To $FileList[0] ;List_IFIX Pictures
GUICtrlSetData($picList, $FileList[$i])
Next
;run
Case $run
If _GUICtrlListBox_GetCount($picList) = 0 Then
MsgBox(0, "", "No Files Found.")
Else
For $i = 1 To $FileList[0]
If Not WinExists("AutoIT Help]") Then
ShellExecute($source & "\" & $FileList[1])
_Expand($hWnd, $hChild)
EndIf
Next
EndIf
;exit
Case $GUI_EVENT_CLOSE
ExitLoop
EndSwitch
WEnd
EndIf
And here is my function:
Func _Expand($hWnd, $hChild)
WinWaitActive("AutoIT Help")
MsgBox(0,"","Expand")
While 1
$hChild = _GUICtrlTreeView_GetNextChild($hWnd, $hChild)
If _GUICtrlTreeView_GetText($hWnd, $hChild) = "Tutorials" Then ExitLoop
WEnd
_GUICtrlTreeView_Expand(ControlGetHandle("[CLASS:HH Parent;TITLE:AutoIt Help]","", "[CLASS:SysTreeView32; INSTANCE:1]"),$hchild, True)
EndFunc
Lots of problems with the code.
Check your titles! In two cases you have got the window title spelt incorrectly in your example. AutoIt is spelt with a lowercase t, and window title matching is case sensitive unless you set an option otherwise.
If "Tutorials" is not found then you will loop forever. You should add a check after _GUICtrlTreeView_GetNextChild to see if you've reached the end of the treeview.
But the real problem with your code is that you are setting $hWnd and $hChild at the beginning of the code, before you run the process that creates the window. As a result, the window is not found, and so $hWnd will always be NULL when you call _Expand.
This sort of question is not encouraged on stackoverflow. We like questions which will be useful to other people in the future rather than help with specific code. Before asking questions like this in the future, please try and debug the problem yourself. You could add ConsoleWrite statements through the code showing variable values, which would have shown you that $hWnd didn't have a handle value when you enter expand, from there it is obvious.

Faster and less cheesy matching of items from 2 collections

Hi I need to iterate two collections containing different object types and do some matching, adding matched items to a 3rd list.
private CheesyMatch( BindingList< MyTypeA > theListA, BindingList< MyTypeB > theListB )
{
foreach( MyTypeA item in theListA )
{
foreach( MyTypeB item2 in theListB )
{
if( item.name == item2.name )
{
item.matched = true;
item2.matched = true;
MyMatchedList.items.add( new matchedItem( item, item2 ) );
}
}
}
}
Is there a better/more efficient way to do this? (I have simplified things a little, as I have some code in my code that copies to new local collections before iterating them, as I was having threading issues.
Not sure what language this is, but there must be some method liked "exists" or "contains" where you do two loops in series. In pseudo code
foreach item in ListA
if ListB.exists(item) then
MatchedList.items.add(item)
end if
endfor
foreach item in ListB
if ListA.exists(item) then
MatchedList.items.add(item)
end if
endfor
That way you only go over each collection once rather than doing ListB N times where ListA has N items. Does this make sense?

Resources