Jump to content

Wikipedia New Example Challenge


czardas
 Share

Recommended Posts

  • Moderators

 

What are your thoughts on or experiences with a GUI jam packed with every example available? 

 

That isn't a bad thought. It is similar to what Valuator did in AutoIt1-2-3, and I remember that being visually impressive for beginners.

"Profanity is the last vestige of the feeble mind. For the man who cannot express himself forcibly through intellect must do so through shock and awe" - Spencer W. Kimball

How to get your question answered on this forum!

Link to comment
Share on other sites

Remember this has to be simple code for general readership, like the example from Manadar. It doesn't require great coding skill to write something like this. Here's another simple example also using notepad. Everyone should be able to relate to it.

;

; Type the user's age into notepad.

; Create an input box and ask the user to type their age.
Local $sAge = InputBox("Title", "What is your age?")

; Check to see if an error occured, and if so then exit.
If @error Then Exit

; Check that the user typed a number, and if not then exit.
If Not StringIsDigit($sAge) Then Exit

; Run notepad.
Run("notepad.exe")

; Wait for notepad to become active.
WinWaitActive("Untitled - Notepad")

; Automatically type the users age into notepad.
Send("You are " & $sAge & " years old.")
Edited by czardas
Link to comment
Share on other sites

This is not an entry.

The German forum aimed to redo bad examples and add new to the awesome and very helpful Rosetta Code project. Our progress has not yet been uploaded to RC, but i will do this soon (don't trust me…).

Anyway, I want to suggest name22 implementation of "Forest Fire" as a candidate. While it is more advanced than some beginners might like, I figure it represents a good average in terms of complexity.

name22 "Forest Fire" (+ Link to Task): https://autoit.de/index.php/Thread/37242-RosettaCode-Sammelthread/?postID=299382#post299382

AutoIt RC code: http://rosettacode.org/wiki/Category:AutoIt

Edited by minx

I will answer every single PM, and you are free to ask anything anytime.

Link to comment
Share on other sites

@minx - While I haven't looked at the code, and are just going by your words, it appears you misunderstand the purpose of this.

The code is not meant to be complex at all.

It is meant to show the power of AutoIt off, while at the same time being understandable to a budding programmer or someone considering programming. It will require them to think a little of course, but you would expect nothing else from someone considering taking up programming. They would need a certain level of intelligence and be capable of using logic.

Make sure brain is in gear before opening mouth!
Remember, what is not said, can be just as important as what is said.

Spoiler

What is the Secret Key? Life is like a Donut

If I put effort into communication, I expect you to read properly & fully, or just not comment.
Ignoring those who try to divert conversation with irrelevancies.
If I'm intent on insulting you or being rude, I will be obvious, not ambiguous about it.
I'm only big and bad, to those who have an over-active imagination.

I may have the Artistic Liesense ;) to disagree with you. TheSaint's Toolbox (be advised many downloads are not working due to ISP screwup with my storage)

userbar.png

Link to comment
Share on other sites

  • Moderators

Not to mention the purpose was for each member to submit something they would like considered, not for people to just find random code on the forum and post a link. If you have nothing to submit of your own work, that is fine, but please don't bog down the thread with links to others' work.

"Profanity is the last vestige of the feeble mind. For the man who cannot express himself forcibly through intellect must do so through shock and awe" - Spencer W. Kimball

How to get your question answered on this forum!

Link to comment
Share on other sites

Multiple fetches from multiple places is also fun.  This cannot be the winner as the source for the sites has no guarantee of permanence (nor permission to automate), but this is the idea in my head.  Get some array and string fun, with some _inet and macro action...

#include <Inet.au3>

$sSource1 =  _INetGetSource("http://www.infosniper.net")

$aIP = stringregexp($sSource1 , "for the IP address(.*?)and" , 3)
$sIP = $aIP[0]

$aLatLong= stringregexp($sSource1 , "new GLatLng\((.*?)\)" , 3)
$sLatLon = stringreplace($aLatLong[0] , "," , "%2C")

$sSource2 =  _INetGetSource("http://www.wunderground.com/cgi-bin/findweather/hdfForecast?query=" & $sLatLon)

$aWeather = stringregexp($sSource2 , '<meta property="og:title" content="(.* \| .*&deg. \| .*)" \/\>' , 3)
$aSplit = StringSplit($aWeather[0] , "|" , 2)
$aSplit[1] = stringtrimright($aSplit[1] , 6)

$sLocation = $aSplit[0]
$sTemp = $aSplit[1]
$sSky = $aSplit[2]


msgbox(0, '' , "Hello " & @UserName & ". Your Public IP is " & $sIP & " located at " & $aLatLong[0] & ". The current weather for " & $sLocation & " is " & $sSky & " and " & $sTemp & " degrees farenheit.")
Edited by boththose

,-. .--. ________ .-. .-. ,---. ,-. .-. .-. .-.
|(| / /\ \ |\ /| |__ __||| | | || .-' | |/ / \ \_/ )/
(_) / /__\ \ |(\ / | )| | | `-' | | `-. | | / __ \ (_)
| | | __ | (_)\/ | (_) | | .-. | | .-' | | \ |__| ) (
| | | | |)| | \ / | | | | | |)| | `--. | |) \ | |
`-' |_| (_) | |\/| | `-' /( (_)/( __.' |((_)-' /(_|
'-' '-' (__) (__) (_) (__)

Link to comment
Share on other sites

Today is the final day for entries. Please make sure you get them to jaberwacky before midnight local time. People living on Howland Island get an extra four hours because of the International Date Line. Thanks for the entries received so far, although I haven't seen any of them yet. Also thanks to everyone involved. :)

Edited by czardas
Link to comment
Share on other sites

And the Winner Is?

After some discussion, a winner has been selected. All the entries were good, but the one chosen was considered the most suitable, providing that we be allowed to change the names of one or two variables. The code itself needs no alteration. I liked the use of VarGetType() as a return value when an error occurs. I thought that was a nice touch.

;

1st Place

_Example()

Func _Example()
    ;Gather some numbers to find the average of.
    Local $sInput = InputBox("Find Average", "enter some numbers deliminated with a comma (,)")

    ;Turn those numbers into an array.
    Local $aSplit = StringSplit($sInput, ",", 2)

    ;Pass the array to the _Find_Average function
    Local $nAverage = _Find_Average($aSplit)

    ;Check for errors
    If @error Then
        MsgBox(0, "Error", "Err: " & @error & @CRLF & "Variable passed: " & $nAverage)
        Exit
    EndIf
    ;Show the result on screen
    MsgBox(0, "Find Average", "Result: " & $nAverage)
EndFunc   ;==>_Example

Func _Find_Average(ByRef $aInput)
    ;Check Input is of correct Type (an array)
    ;Other checks can be performed but for brevity
    ; just this one is performed here.
    If Not IsArray($aInput) Then Return SetError(1, 0, VarGetType($aInput))

    ;Declare Upper Bound of array, and Sum variables.
    Local $iUbound = UBound($aInput)
    Local $nArraySum = 0

    For $i = 0 To $iUbound - 1
        ;Increment the sum by array values.
        $nArraySum += Number($aInput[$i])
    Next

    ;Return average rounded to 2 decimal places.
    Return Round($nArraySum / $iUbound, 2)
EndFunc   ;==>_Find_Average 

;

The runner up also posted a great example. I thought it was very well written and commented. The only reason it did not win was because it was considered too advanced for the article in question. I found it hard to choose one.

;

2nd Place

; Script Function: Example of GUI Form Creation, Arrays, Reading and Writting to Disk

#Include <GUIConstantsEx.au3>; Defines GUI_EVENT_CLOSE= -3
#Include <file.au3>; Defines FO_OVERWRITE= 2

Global Enum $eRecord_Data, $eRecord_Label; Used to Address Record Array Elements
main()

; Main() Create Record GUI Controls.  Allow User to ModifiyChange Fields and Save to disk
Func main()
    Local $hGui= GUICreate(@ScriptName, 380, 180); Create GUI Dialog Window and Return a Handel to the Window
    Local $aiGui_Rect= WinGetPos($hGui); Return Array of GUI Window Data [0 X, 1 Y, 2 Width, 3 Height]
    Local $sRecord_Path= @ScriptDir&"\Record.txt"
    Local $idSave= GUICtrlCreateButton("Save", $aiGui_Rect[2]-80, $aiGui_Rect[3]*.45, 50, 20); Use the GUI Width Element to position "Save" Button
    Local $bSave_Record= 0; Toggles Record File Save Event
    Local $asRecord_Name= ["First Name", "Last Name", "Phone", "Zip", "Medication"]; Array of Label Field Strings to Add a Field Simply Include another String
    Local $iRecord_Data_Max= 2; Data and Label Control for Each Record.  See Global Enum $eRecord_Data, Label
    Local $aidRecord[ UBound($asRecord_Name) ][$iRecord_Data_Max]; The Amount of Records Equals Amount of Elements in asRecord_Name[..]

    record_control_create($aidRecord, $asRecord_Name, $sRecord_Path); Create Record Controls.  Load Stored Records From File.
    GUISetState(); Shows the Last GUI Created
    Do; START Main Loop to capture User Record Changes
        $msg= GUIGetMsg(); Returns GUI Dialog Events
        If $msg= $idSave then
            $bSave_Record= 1; Flag the Save Event
            ExitLoop; Leave Main Loop
        EndIf;msg= $control_save
    Until $msg= $gui_event_close; END Main Loop
    If $bSave_Record= 1 Then record_save($aidRecord, $sRecord_Path); Save the Record to File @ScriptDir&"\Record.txt"
EndFunc;main()

; Create the Record GUI Controls and Load Record File
Func record_control_create(ByRef $aidRecord, $asRecord_Name, $sRecord_Source_Path)
    Local $iRecord_Control_Height= 20, $iRecord_Control_Label_X= 15, $iRecord_Control_Data_X= 150, $iRecord_Control_Data_Width= 90, $iRecord_Control_Indent_Y= 15; Control Positioning Variables
    $file= FileOpen($sRecord_Source_Path); Default Open for Reading
    for $i= 0 to UBound($aidRecord)-1; Loop through All Records
        $aidRecord[$i][$eRecord_Label]= GUICtrlCreateLabel($asRecord_Name[$i], $iRecord_Control_Label_X, $iRecord_Control_Indent_Y + $i * $iRecord_Control_Height); Label the Controls Using a_record_name[]
        $aidRecord[$i][$eRecord_Data]=  GUICtrlCreateInput(FileReadLine($file), $iRecord_Control_Data_X, $iRecord_Control_Indent_Y + $i * $iRecord_Control_Height, $iRecord_Control_Data_Width, $iRecord_Control_Height-1); Fill Control with File Data or BLANK IF NO FILE
    next;i in record
    FileClose($file)
EndFunc;record_control_create()

; Save Record Data to File
Func record_save($aidRecord, $sDestination_Filepath)
    $file= FileOpen($sDestination_Filepath, $FO_OVERWRITE); Open For Writing
    for $i= 0 to UBound($aidRecord)-1; Loop through All Records
        FileWriteLine( $file, GUICtrlRead($aidRecord[$i][$eRecord_Data]) ); Reads Data Control, Writes to Disk
    next;i in record[i]
    FileClose($file)
EndFunc;record_save()

;

As soon as jaberwacky has revealed the winner's name, they will be contacted.

Link to comment
Share on other sites

Well done and congratulations to both of you. Thanks for your participation, and thanks also to jaberwacky.

JohnOne, I was going to give you a trophy, but I see you have one already.  ;)

Maybe you can have two trophies.  :dance:

I sent you a PM.

Edited by czardas
Link to comment
Share on other sites

My suggested modifications are really just small changes. It is uncertain if the user will enter a float, but assuming they only input digits and commas we can use the prefix ($i or maybe $v) for $nArraySum. I also use $n for undetermined number type, but it's never been official. I also added back constants. The comments can be changed again if there's something you're not happy with.

;

#include <MsgBoxConstants.au3>
#include <StringConstants.au3>

_Example()

Func _Example()
    ; Ask the user to enter some numbers into an input box.
    Local $sInput = InputBox("Find Average", "Enter some numbers separated by commas (,)")

    ; Populate an array with the numbers.
    Local $aSplit = StringSplit($sInput, ",", $STR_NOCOUNT)

    ; Pass the array to the function _Find_Average().
    Local $fAverage = _Find_Average($aSplit)

    ; Check for errors
    If @error Then
        ; Display an error message and then exit the script.
        MsgBox($MB_OK, "Error", "Err: " & @error & @CRLF & "Variable passed: " & $fAverage)
        Exit
    EndIf

    ; Display the result in a message box.
    MsgBox($MB_OK, "Find Average", "Result: " & $fAverage)
EndFunc   ;==>_Example

Func _Find_Average($aInput)
    ; Check the input is of the correct type (an array) and if not, return an error.
    If Not IsArray($aInput) Then Return SetError(1, 0, VarGetType($aInput))

    ; Declare the upper bound of the array, and a variable to store the sum of the numbers.
    Local $iUbound = UBound($aInput)
    Local $iArraySum = 0

    ; Loop through the array.
    For $i = 0 To $iUbound - 1
        ; Increment the sum by the number in each array element.
        $iArraySum += Number($aInput[$i])
    Next

    ; Return the average rounded to 2 decimal places.
    Return Round($iArraySum / $iUbound, 2)
EndFunc   ;==>_Find_Average

;

Edit: I just noticed that ByRef is not needed: because the array is not modified within the function and therefore no copy of the array is created.

Edited by czardas
Link to comment
Share on other sites

Congratulations JohnOne. :thumbsup:

Now if only I'd placed that bet. :

No doubt all the religious talk lately has rubbed off and made me prophetic ... or pathetic .... take your choice. o:)

Make sure brain is in gear before opening mouth!
Remember, what is not said, can be just as important as what is said.

Spoiler

What is the Secret Key? Life is like a Donut

If I put effort into communication, I expect you to read properly & fully, or just not comment.
Ignoring those who try to divert conversation with irrelevancies.
If I'm intent on insulting you or being rude, I will be obvious, not ambiguous about it.
I'm only big and bad, to those who have an over-active imagination.

I may have the Artistic Liesense ;) to disagree with you. TheSaint's Toolbox (be advised many downloads are not working due to ISP screwup with my storage)

userbar.png

Link to comment
Share on other sites

Edit: I just noticed that ByRef is not needed: because the array is not modified within the function and therefore no copy of the array is created.

I thought a copy operation was carried out always when not ByRef.

Oh well, you learn something new every day.

EDIT:

I'll use a quote from the help file in my defence  :whistle:

Note that AutoIt only copies an array parameter if the contents are changed, so it is only in this case that ByRef offers an advantage, although it is recommended that to use it in all cases.

 

Edited by JohnOne

AutoIt Absolute Beginners    Require a serial    Pause Script    Video Tutorials by Morthawt   ipify 

Monkey's are, like, natures humans.

Link to comment
Share on other sites

Congratulations JohnOne. :thumbsup:

 

Now if only I'd placed that bet. :

 

No doubt all the religious talk lately has rubbed off and made me prophetic ... or pathetic .... take your choice. o:)

I never entered a punt until about 10 hours to go, never even noticed the thread  : Edited by JohnOne

AutoIt Absolute Beginners    Require a serial    Pause Script    Video Tutorials by Morthawt   ipify 

Monkey's are, like, natures humans.

Link to comment
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
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...