Sign in to follow this  
Followers 0
litlmike

Modifying this For Loop

9 posts in this topic

I am pretty sure there is another way to write this Loop without using an If Statement, can you help me figure out how? I need to avoid the If statement, because I need to Return $Row, but this makes it equal 0 as it exits the loop. Thanks in advance.

Func Put_Table_Data_Excel($aTableData, $Row , $Column = 0, $iBase = 0)
        For $iCC = $iBase To UBound($aTableData, 1) - 1
            $Column+=1
        
            For $xCC = 0 To UBound($aTableData, 2) - 1
                $Row +=1
                $Strip_CR = StringSplit ($aTableData[$iCC][$xCC], @CR)
                $oExcel.ActiveWorkBook.ActiveSheet.Cells($Row,$Column).Value=$Strip_CR[1]
            Next

            If ($xCC = UBound($aTableData, 2)) Then $Row = 0
        Next
EndFunc

Share this post


Link to post
Share on other sites



#2 ·  Posted (edited)

($xCC = UBound($aTableData, 2) - 1)? ... Edit... Maybe I'm not understanding, obviously that's going to make it 0 too... is there a reason that $xCC would = UBound($aTableData,2) ... if so, that may be what you want to look at.

Edited by SmOke_N

[center]Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.[/center]

Share this post


Link to post
Share on other sites

($xCC = UBound($aTableData, 2) - 1)? ... Edit... Maybe I'm not understanding, obviously that's going to make it 0 too... is there a reason that $xCC would = UBound($aTableData,2) ... if so, that may be what you want to look at.

Yea that was my ghetto trial and error way to get the values to print in excel properly. It was the only way I could think of entering all the data "vertically" then moving to the next column to repeat. Any ideas how else to do this?

Share this post


Link to post
Share on other sites

Here is what I think my script is saying right now:

For 0 To the Number of elements in the array (1st Dimension - 1) ; This is to go from row 0 to the end
    Add 1 to Column (From 0)

    For 0 to the number of elements in the 2nd dimension of that array (-1) ;This is to go from column 0 to the end
        Add 1 to the Row (From 0)
        Split the String after the @CR and save it to a var.
        The Excel Cell at Row/Column = The Var above
    

    If (When) $xCC gets to the Upper Limit, then make $Row = 0 to start back at the top

How do I say that without the If statement.... I feel like I am not making the question clear am I?

Share this post


Link to post
Share on other sites

#5 ·  Posted (edited)

The if statement is redundant... as it's going to go the top regardless ... and you've set no conditions to exit the 2nd loop if something is found.

This:

Func Put_Table_Data_Excel($aTableData, $Row , $Column = 0, $iBase = 0)
        For $iCC = $iBase To UBound($aTableData, 1) - 1
            $Column+=1
       
            For $xCC = 0 To UBound($aTableData, 2) - 1
                $Row +=1
                $Strip_CR = StringSplit ($aTableData[$iCC][$xCC], @CR)
                $oExcel.ActiveWorkBook.ActiveSheet.Cells($Row,$Column).Value=$Strip_CR[1]
            Next

            If ($xCC = UBound($aTableData, 2)) Then $Row = 0
        Next
EndFuncoÝ÷ Ø-ëé赧-¹©eÉ·jëÓ~±,"YÞ½ê
If this doesn't answer your question, then I definitely do not know what you are asking.

Edit:

I am curious on what this line does for you though:

$Strip_CR = StringSplit($aTableData[$iCC][$xCC], @CR)

Edited by SmOke_N

[center]Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.[/center]

Share this post


Link to post
Share on other sites

#6 ·  Posted (edited)

The if statement is redundant... as it's going to go the top regardless ... and you've set no conditions to exit the 2nd loop if something is found.

Ahh... I think that is helpful, just tweaking it a bit, I think this does it. But that will leave me with that question answered, and give me 1 new question. How do I return a value, that I want to use to input into that SAME function later on?... I notated that line in the code below.

I have 4+ functions that will call Get_Table_Data, which then calls Put_Table_Data_Excel. So, after the 1st function completes I now will need to save/return $Row for use with the next fun, which in turn will call Get_Table_Data and Put_Table_Data_Excel. Then I will need to re-return/save $Row updated value until the last Function completes. For example, $Row will start out at 1 for the first function, then maybe 20 for the next Func, then maybe 50 for the 3rd Func. That value will have to be returned and entered somehow.... Since you taught me how to use Return, I am loving it! But I still have some gaps in understanding it.

Thanks!

So here is your code tweaked:

Note: About the @CR line, the data I am grabbing has 2 lines of text, the 1st I need, the 2nd I don't so I just used StringSplit to delimit the 1st and 2nd part of the string. Then I only input the first element of the StringSplit array.

***EDIT****

I am starting to wonder if maybe it is easiest to declare $row as a Global variable.

Func Get_Table_Data ($oIE)
    $iCC = 4
    $oTable = _IETableGetCollection ($oIE, $iCC)
    $aTableData = _IETableWriteToArray ($oTable)
    $Row = Put_Table_Data_Excel($aTableData) ;This might cause a problem
EndFunc

Func Put_Table_Data_Excel($aTableData, $Row = 1, $Column = 1, $iBase = 0, $iCC = 0)         ;;This line will have to change
;~  $oExcel = Create_Excel ()
        
    For $iCC = $iBase To UBound($aTableData, 1) - 1
        $Row = 1 ;This line will have to change
        For $xCC = 0 To UBound($aTableData, 2) - 1
            $Strip_CR = StringSplit ($aTableData[$iCC][$xCC], @CR)
            $oExcel.ActiveWorkBook.ActiveSheet.Cells($Row,$Column).Value=$Strip_CR[1]
            $Row +=1
        Next
        $Column+=1
    Next
    Return $Row
            
EndFunc
Edited by litlmike

Share this post


Link to post
Share on other sites

?

Func Get_Table_Data ($oIE)
    $iCC = 4
    $oTable = _IETableGetCollection ($oIE, $iCC)
    $aTableData = _IETableWriteToArray ($oTable)
    $Row = Put_Table_Data_Excel($aTableData) ;This might cause a problem
EndFunc

Func Put_Table_Data_Excel($aTableData, $Row = 1, $Column = 1, $iBase = 0, $iCC = 0)         ;;This line will have to change
;~  $oExcel = Create_Excel ()
    Local $nSaveRow = 0
    For $iCC = $iBase To UBound($aTableData, 1) - 1
        $Row = 1 ;This line will have to change
        For $xCC = 0 To UBound($aTableData, 2) - 1
            $Strip_CR = StringSplit ($aTableData[$iCC][$xCC], @CR)
            If IsArray($Strip_CR) Then $oExcel.ActiveWorkBook.ActiveSheet.Cells($Row,$Column).Value = $Strip_CR[1]
            $Row +=1
        Next
        $Column+=1
        $nSaveRow += $Row
    Next
    Return $nSaveRow
EndFunc


[center]Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.[/center]

Share this post


Link to post
Share on other sites

Bah! I feel like my brain is going to explode.... your last idea didn't quite work, but I think it was close, but I couldn't bridge the gap. I did come up with "A" solution, but maybe not "THE" solution. Maybe at least showing you "a" solution will clarify what the heck I am trying to do. Man, you've been so helpful.

Global $Row = 1
Global $Row2 = 1

Func Get_Table_Data ($oIE)
    $iCC = 4
    $oTable = _IETableGetCollection ($oIE, $iCC)
    $aTableData = _IETableWriteToArray ($oTable)
    Put_Table_Data_Excel($aTableData)
EndFunc

Func Put_Table_Data_Excel($aTableData,  $Column = 1, $iBase = 0, $iCC = 0)
        
    For $iCC = $iBase To UBound($aTableData, 1) - 1
        $Row = $Row2
        For $xCC = 0 To UBound($aTableData, 2) - 1
            $Strip_CR = StringSplit ($aTableData[$iCC][$xCC], @CR)
            $oExcel.ActiveWorkBook.ActiveSheet.Cells($Row,$Column).Value=$Strip_CR[1]
            $Row +=1
        Next
        $Column+=1
    Next
       $Row2 = $Row         
EndFunc

Share this post


Link to post
Share on other sites

Globals are not necessarily a bad thing litlmike.

If you are trying to pas the data by parameters, I suppose you could do it this way:

Global $nOldRow = 1

Func Get_Table_Data ($oIE)
    $iCC = 4
    $oTable = _IETableGetCollection ($oIE, $iCC)
    $aTableData = _IETableWriteToArray ($oTable)
    $nOldRow = Put_Table_Data_Excel($aTableData, $nOldRow)
EndFunc

Func Put_Table_Data_Excel($aTableData, $nOldRow, $Column = 1, $iBase = 0, $iCC = 0)
    Local $Row
    For $iCC = $iBase To UBound($aTableData, 1) - 1
        $Row = $nOldRow
        For $xCC = 0 To UBound($aTableData, 2) - 1
            $Strip_CR = StringSplit ($aTableData[$iCC][$xCC], @CR)
            $oExcel.ActiveWorkBook.ActiveSheet.Cells($Row,$Column).Value=$Strip_CR[1]
            $Row +=1
        Next
        $Column+=1
    Next
    Return $Row  
EndFunc
But if yours works, I don't see the issue with it.

[center]Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.[/center]

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0