Sign in to follow this  
Followers 0
Neoborn

Dynamic Array Redim?

12 posts in this topic

#1 ·  Posted (edited)

Hey guys,

So here I am ever on my journey to learn more about AutoIT and how to program.

Currently I am working on a small proggie that just runs through a bunch of emails from Question of the day site which emails me a question a day on whatever techie subject I choose. Because I don't have time daily to do these I prefer to go through and just harvest all the site links in one go. So, here's my pseudo code:

1. Open email

2. Search for link

3. Copy link

4. Currently write to file

What I would prefer to do is send this copied link to a variable and then to an array(not always sure how to explain this as I am self taught). I would like to know how to do this as I'm pretty clueless with arrays.

#Include <File.au3>

Opt("WinTitleMatchMode", 4)

HotKeySet("^e", "EmailWin")

Global $title = "QOD - Thunderbird", $title1 = "Question of the day", $find = "Find in This Page", $n = 0, $html = FileOpen(@DesktopDir & "\Question Of The Day.html", 1), $Clip = ClipGet(), $http = "http://"

While 1
    Sleep(300)
WEnd
;~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~Functions~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~;
Func EmailWin();This function just checks to see if the window exists, activates, then calls the Html function then the Repeat function
    If WinExists($title) = 0 Then
        MsgBox(48, "Error", "The window " & $title & " does not exist!")
    ElseIf WinExists($title) = 1 Then
        WinActivate($title)
        WinWaitActive($title)
        Html()
        Repeat()
    EndIf
EndFunc   ;==>EmailWin

Func Html(); This function just creates an html file and inserts this line
    FileWriteLine($html, "<html>" & @CRLF & "<title>InsertTitleHere</title>" & @CRLF & "<body>")
    Return (1)
EndFunc   ;==>Html

Func Repeat();This is basically the large chunk of operations i.e. open the email, copy the link and then write to file. Prefer to write all to an array then once done write to a file in one go as file access is slow;
    While $n < 2
        Send("{ENTER}")
        WinWait($title1)
        Send("^f")
        WinWait($find)
        ClipPut("www.qod.us")
        Send("^v")
        Send("!f{ESC}")
        Send("+{END}^c")
        FileWriteLine($html, "<center><a href=""" & $http & $Clip & """>Question Of The Day Link " & $n & "</a><br>" & @CRLF)
        Send("{ESC}")
        Send("{DOWN}")
        $n = $n + 1
    WEnd
    FileClose($html)
EndFunc   ;==>Repeat

Basically I would like to know:

1. Would it be best to start with an array say $array[100] and then write to it and if I get to the end +1 it?

2. Would it be better to just start with $array[1] and then on every loop redim the array and increase it by +1?

3. Once I am done harvesting all the links I am going to want to write my array to the html file in the format shown above in the filewrite line part of the Repeat() func.

Please feel free to go into depth as I am a total noob about arrays and barely grasp the concept even though I may sound like I know something :D

Thanks guys for any help in advance.

Edited by Neoborn

~Projects~1. iPod Ejector 1.0 - Tool Used To Eject iPod in Windows - Uses DevEject.exe :P2. SmartFTP Close Popup Tool - Closes reminders from freeware SmartFTP.~Helpful Links For New Users~1. LXP's Learning AutoIT PDF Guide - <<< Go here for a PDF Guide on learning AutoIT from the ground up!<<<2. AutoIt 1-2-3 <<<Want to learn more about AutoIT quickly? Go Here<<<3. How To Install The Beta And Production Versions Of AutoIT / SciteAutoIT

Share this post


Link to post
Share on other sites



Hi,

because there is no need for 100 % performance, I'll suggest option 2. Redim every time in a loop.

So long,

Mega


Scripts & functions Organize Includes Let Scite organize the include files

Yahtzee The game "Yahtzee" (Kniffel, DiceLion)

LoginWrapper Secure scripts by adding a query (authentication)

_RunOnlyOnThis UDF Make sure that a script can only be executed on ... (Windows / HD / ...)

Internet-Café Server/Client Application Open CD, Start Browser, Lock remote client, etc.

MultipleFuncsWithOneHotkey Start different funcs by hitting one hotkey different times

Share this post


Link to post
Share on other sites

Do you have an example of some sort or can you tell me what I should read in order how to figure this out? Never really made an array before.

Hi,

because there is no need for 100 % performance, I'll suggest option 2. Redim every time in a loop.

So long,

Mega


~Projects~1. iPod Ejector 1.0 - Tool Used To Eject iPod in Windows - Uses DevEject.exe :P2. SmartFTP Close Popup Tool - Closes reminders from freeware SmartFTP.~Helpful Links For New Users~1. LXP's Learning AutoIT PDF Guide - <<< Go here for a PDF Guide on learning AutoIT from the ground up!<<<2. AutoIt 1-2-3 <<<Want to learn more about AutoIT quickly? Go Here<<<3. How To Install The Beta And Production Versions Of AutoIT / SciteAutoIT

Share this post


Link to post
Share on other sites

If you know in advance that that array is going to be a certain size, then I would declare it as that size to save the penalty of constant ReDimming.

An example of ReDim from 10 to 22.

; Declare the array
Global $array[10]

; Let us use 21 elements in the array
For $i = 0 To 20
    ; Assign a value to an array element
    $array[$i] = 'string'
    ; Check the array size incase ReDim is needed
    If $i = (UBound($array)-1) Then
        ; ReDim the array
        ReDim $array[$i+2]
    EndIf
Next

; Display the array size as of now
MsgBox(0, '', 'Global $array[' & UBound($array) & ']')

:D

Share this post


Link to post
Share on other sites

#5 ·  Posted (edited)

Ok so here's my final result, a few things also that I would like to know:

1. This worked fine but for some reason I don't know of, the script would right to the file but always add on 92 entries on in addition to what it was writing into the file. For example I have 92 emails so I set my array size as [93], not sure on how to explain this but I am guessing I should have used UBound to find out the size of the array and then write that many lines to the file.

2. Is there anything you guys would do to clean up this code?

3. Is my indentation up to par?

4. Local is locally to that function, & Global is throughout the entire code?

Thanks guys.

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;                   Question Of The Day Script From http://www.qod.us                       ;
;                                                                                           ;
;                                                                                           ;
;   Created By:     Neoborn                                                         ;
;   Create Date:    July 2006                                                               ;
;   Credits:        th.meger & MHz for their help & dedication myself and the community     ;
;                   All the other people who help me at www.autoitscript.com/forum          ;
;   ForumLink:      http://www.autoitscript.com/forum/index.php?act=ST&f=2&t=29606          ;
;øøøøøøøøøøøøøøøøøøøøøøøøøøøøøøøøøøøøøøøøøøøøøøøøøøøøøøøøøøøøøøøøøøøøøøøøøøøøøøøøøøøøøøøøøøø;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;     Pseudo code                                                                           ;
;   1. This program goes through the QOD folder and each email to retrieve the link         ;
;      to the website and question of the day.                                              ;
;                                                                                           ;
;   3. This way I can go through many questions I have received in one go rather than one   ;
;      by one                                                                               ;
;                                                                                           ;
;øøøøøøøøøøøøøøøøøøøøøøøøøøøøøøøøøøøøøøøøøøøøøøøøøøøøøøøøøøøøøøøøøøøøøøøøøøøøøøøøøøøøøøøøøøø;

#Include <Array.au3>

Opt("WinTitleMatchMode", 1)

HotKeySet("^e", "QOD"); Set hotkey to ctrl-e to launch specified function

Global $title = "QOD - Thunderbird", $find = "Find in This Page", $file = FileOpen(@DesktopDir & "\Question Of The Day.html", 1), $title1 = "Question of the day", $title2 = "[SPAM] Question of the day", $www = "http://", $cg = ClipGet(), $cp = ClipPut($cg), $ai = 1, $o = 0, $myarray[93]; Variables set globally for use

;~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~Sleep Until Hotkey~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~;
While 1
    Sleep(250)
WEnd
;~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~;
;     Functions
;~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~;
Func QOD(); This function 1)activates the window 2)Finds the link in email 3)Copies link 4)Inserts link into array.
    Local $n = 0
    
    WinActivate($title)
    
    While $n <> 92
        Send("{ENTER}")
        WinWaitActive("")
        Send("^f")
        WinWait($find)
        ClipPut("www.qod.us")
        Send("^v")
        Send("!f{ESC}")
        Send("+{END}")
        Send("^c")
        _ArrayInsert($myarray, $ai, ClipGet())
        Send("{ESC}")
        Send("{DOWN}")
        $n = $n + 1
        $ai = $ai + 1
    WEnd
    html()
EndFunc   ;==>QOD
;~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~;
Func html(); This function just creates an html file and inserts this array elements in line by line
    
    Local $n = 0
    
    FileWriteLine($file, "<html>" & @CRLF & "<title>Question Of The Day</title>" & @CRLF & "<body>")
    
    While $n <> $myarray
        FileWriteLine($file, "<center><a href=""" & $www & $myarray[$n] & """>Question Of The Day" & $n & "</a><br>")
        $n = $n + 1
    WEnd
    Return (1)
    FileClose($file)
EndFunc   ;==>html
;~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~;
Edited by Neoborn

~Projects~1. iPod Ejector 1.0 - Tool Used To Eject iPod in Windows - Uses DevEject.exe :P2. SmartFTP Close Popup Tool - Closes reminders from freeware SmartFTP.~Helpful Links For New Users~1. LXP's Learning AutoIT PDF Guide - <<< Go here for a PDF Guide on learning AutoIT from the ground up!<<<2. AutoIt 1-2-3 <<<Want to learn more about AutoIT quickly? Go Here<<<3. How To Install The Beta And Production Versions Of AutoIT / SciteAutoIT

Share this post


Link to post
Share on other sites

While $n <> $myarray
        FileWriteLine($file, "<center><a href=""" & $www & $myarray[$n] & """>Question Of The Day" & $n & "</a><br>")
        $n = $n + 1
    WEnd
oÝ÷ Ûú®¢×¢êî²)àÓ~¦Éªëk&¬jö«¦åy«0zYZ±©Ú®¶²èSå¡Wwe¢lÂ¥u·zÛ^­«¢³^ÆÙh¢²Ø^«mz¶¥­æÊ뱫ڮ&ëhr§´LZ^jëh×6
Global $array[10]
For $i = 0 To UBound($array) -1
    MsgBox(0, 'Element', $i, 1)
Next

; replaces your concept of

Global $array[10]
Global $i = 0
While $i <> (UBound($array) -1)
    MsgBox(0, 'Element', $i, 1)
    $i = $i + 1
WEnd

And do consider using UBound to get the size of the array as shown.

:whistle:

Share this post


Link to post
Share on other sites

1. um am bit of noob so still learning. I wanted to say when $n <> the size of my array do whatever what is the way I should have expressed that?

2. Why is for next better? in what way?

3. Why use UBound to get the size of the array?

Thanks doodzors :)

Your using $myarray as a variable, as well as an array? :whistle:

The While...WEnd loops would be better as For...Next loops as the latter already uses a variable to count.

And do consider using UBound to get the size of the array as shown.

:)


~Projects~1. iPod Ejector 1.0 - Tool Used To Eject iPod in Windows - Uses DevEject.exe :P2. SmartFTP Close Popup Tool - Closes reminders from freeware SmartFTP.~Helpful Links For New Users~1. LXP's Learning AutoIT PDF Guide - <<< Go here for a PDF Guide on learning AutoIT from the ground up!<<<2. AutoIt 1-2-3 <<<Want to learn more about AutoIT quickly? Go Here<<<3. How To Install The Beta And Production Versions Of AutoIT / SciteAutoIT

Share this post


Link to post
Share on other sites

#8 ·  Posted (edited)

You know it would be easier to append each link to a string and then write the file at the end. Skips the array and your only writing once.

Edit: Also you might figure out how Thunderbird stores your email if you look in this directory: %APPDATA%\Thunderbird

(eg. "C:\Documents and Settings\Administrator\Application Data\Thunderbird")

Edited by gamerman2360

Share this post


Link to post
Share on other sites

You know it would be easier to append each link to a string and then write the file at the end. Skips the array and your only writing once.

Edit: Also you might figure out how Thunderbird stores your email if you look in this directory: %APPDATA%\Thunderbird

(eg. "C:\Documents and Settings\Administrator\Application Data\Thunderbird")

1. Why would it be easier?

2. The array is fast, plus it's something I would like to learn.

3. Why does that have meaning, the location of my email?


~Projects~1. iPod Ejector 1.0 - Tool Used To Eject iPod in Windows - Uses DevEject.exe :P2. SmartFTP Close Popup Tool - Closes reminders from freeware SmartFTP.~Helpful Links For New Users~1. LXP's Learning AutoIT PDF Guide - <<< Go here for a PDF Guide on learning AutoIT from the ground up!<<<2. AutoIt 1-2-3 <<<Want to learn more about AutoIT quickly? Go Here<<<3. How To Install The Beta And Production Versions Of AutoIT / SciteAutoIT

Share this post


Link to post
Share on other sites

#10 ·  Posted (edited)

Ok so here's my final result, a few things also that I would like to know:

1. This worked fine but for some reason I don't know of, the script would right to the file but always add on 92 entries on in addition to what it was writing into the file. For example I have 92 emails so I set my array size as [93], not sure on how to explain this but I am guessing I should have used UBound to find out the size of the array and then write that many lines to the file.

2. Is there anything you guys would do to clean up this code?

3. Is my indentation up to par?

4. Local is locally to that function, & Global is throughout the entire code?

Thanks guys.

CODE

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

; Question Of The Day Script From http://www.qod.us ;

; ;

; ;

; Created By: Neoborn ;

; Create Date: July 2006 ;

; Credits: th.meger & MHz for their help & dedication myself and the community ;

; All the other people who help me at www.autoitscript.com/forum ;

; ForumLink: http://www.autoitscript.com/forum/index.ph...f=2&t=29606 ;

;øøøøøøøøøøøøøøøøøøøøøøøøøøøøøøøøøøøøøøøøøøøøøøøøøøøøøøøøøøøøøøøøøøøøøøøøøøøøøøøøøøøøøøøøøøø;

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

; Pseudo code ;

; 1. This program goes through the QOD folder and each email to retrieve the link ;

; to the website and question of the day. ;

; ;

; 3. This way I can go through many questions I have received in one go rather than one ;

; by one ;

; ;

;øøøøøøøøøøøøøøøøøøøøøøøøøøøøøøøøøøøøøøøøøøøøøøøøøøøøøøøøøøøøøøøøøøøøøøøøøøøøøøøøøøøøøøøøøøø;

#Include <Array.au3>

Opt("WinTitleMatchMode", 1)

HotKeySet("^e", "QOD"); Set hotkey to ctrl-e to launch specified function

Global $title = "QOD - Thunderbird", $find = "Find in This Page", $file = FileOpen(@DesktopDir & "\Question Of The Day.html", 1), $title1 = "Question of the day", $title2 = "[sPAM] Question of the day", $www = "http://", $cg = ClipGet(), $cp = ClipPut($cg), $ai = 1, $o = 0, $myarray[93]; Variables set globally for use

;~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~Sleep Until Hotkey~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~;

While 1

Sleep(250)

WEnd

;~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~;

; Functions

;~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~;

Func QOD(); This function 1)activates the window 2)Finds the link in email 3)Copies link 4)Inserts link into array.

Local $n = 0

WinActivate($title)

While $n <> 92

Send("{ENTER}")

WinWaitActive("")

Send("^f")

WinWait($find)

ClipPut("www.qod.us")

Send("^v")

Send("!f{ESC}")

Send("+{END}")

Send("^c")

_ArrayInsert($myarray, $ai, ClipGet())

Send("{ESC}")

Send("{DOWN}")

$n = $n + 1

$ai = $ai + 1

WEnd

html()

EndFunc ;==>QOD

;~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~;

Func html(); This function just creates an html file and inserts this array elements in line by line

Local $n = 0

FileWriteLine($file, "<html>" & @CRLF & "<title>Question Of The Day</title>" & @CRLF & "<body>")

While $n <> $myarray

FileWriteLine($file, "<center><a href=""" & $www & $myarray[$n] & """>Question Of The Day" & $n & "</a><br>")

$n = $n + 1

WEnd

Return (1)

FileClose($file)

EndFunc ;==>html

;~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~;

You seem to assume you will have exactly 93 emails, or is that lines in a single email? And your array starts out with the number of cells required (93, or 0 thru 92), but then you do _ArrayInsert() functions, which increase the size of the array...? :whistle:

If you know ahead of time, somehow, how many emails there will be then just do a For/Next loop that many times:

Func QOD()
    Local $n
    WinActivate($title)
    For  $n = 0 To 92
        Send("{ENTER}")
        WinWaitActive("")
        Send("^f")
        WinWait($find)
        ClipPut("www.qod.us")
        Send("^v")
        Send("!f{ESC}")
        Send("+{END}")
        Send("^c")
        $myarray[$n] = ClipGet()
        Send("{ESC}")
        Send("{DOWN}")
    Next
    html()
EndFunc ;==>QOD

It would be easier to append it to a string, but if you must use an array, that will do it. :)

P.S. Forgot to add the same technique for reading the array:

Func html()
    Local $n
    FileWriteLine($file, "<html>" & @CRLF & "<title>Question Of The Day</title>" & @CRLF & "<body>")
    
    For $n = 0 To 92
        FileWriteLine($file, "<center><a href=""" & $www & $myarray[$n] & """>Question Of The Day" & $n & "</a><br>")
    Next

    FileClose($file)
    Return (1)
EndFunc  ;==>html

You were also doing Return 1 before closing the file, which I figure was unintentional...

Edited by PsaltyDS

Valuater's AutoIt 1-2-3, Class... Is now in Session!For those who want somebody to write the script for them: RentACoder"Any technology distinguishable from magic is insufficiently advanced." -- Geek's corollary to Clarke's law

Share this post


Link to post
Share on other sites

1. Why would it be easier?

2. The array is fast, plus it's something I would like to learn.

3. Why does that have meaning, the location of my email?

1. No arrays/extra code to handle them.

2. If you want to learn about arrays that's good too. Just skip what I said.

3. You could open the mail in AutoIt. You don't have to use the Thunderbird program.

Share this post


Link to post
Share on other sites

1. um am bit of noob so still learning. I wanted to say when $n <> the size of my array do whatever what is the way I should have expressed that?

2. Why is for next better? in what way?

3. Why use UBound to get the size of the array?

Instead of answers your separate questions which seem to point to the same concept of which loop and why UBound(). I will summarize.

UBound() is the only way to get the element count of an array other then an array created with StringSplit. StringSplit holds the count in the 1st element, but if you were to ReDim this array latter, then UBound would be also needed.

When a loop needs to count, then the 1st type of loop I consider is For...Next. With the other loops, you would have to declare a separate variable to count and add an extra line to index the count to the Step that you want. A For...Next loop also has an optional Step to index the count to your liking.

This is a For...Next loop in words incase you do not understand it.

For this variable equals from this set value To that set value (with an optional Step to make the count go up or down to a set value after Step). Your lines of code will be executed within the body of the loop until it reaches Next. It will then return to the 1st line where the variable that stores the count will index. You can also use the variable that stores the count within the body of the loop to index your array also.

If you can understand the above, then you can understand a For...Next loop as it is no more complex then as stated above.

Another small example:

Global $array[10]
For $count = 0 To UBound($array) -1
    MsgBox(0, 'Looping through an array', $array[$count])
Next

-1 is used on the UBound as we declared 10 elements which we use from index 0 to 9. So we want it to stop at 9 to prevent out of bound.

Hopefully not too noobish for you. :whistle:

@gamerman2360 3 sounds good if an easy filetype and access.

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