Jump to content

array


Recommended Posts

this sir, who can help me convert this script to au3

getsma = function(array) {
    var = average = [],
    sum = 0,
    period = array.leght;
    for ( var i = 0; i < period; i++) {
    sum + = array[i];
    }
    average = sum/period;
    return average;
    };

 

Link to comment
Share on other sites

  • Moderators

mendrofa,

Loop through the array adding each element to a total and then divide that total by the number of elements - just simple maths. Use UBound & For...Next loop to do it.

M23

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see my UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Link to comment
Share on other sites

  • Moderators

mendrofa,

The code would look something like this:

#include <Array.au3>
#include <MsgBoxConstants.au3>

Global $aArray[5] = [123, 456, 789, 234, 567]

_ArrayDisplay($aArray, "Array", Default, 8)

$nAverage = _Average($aArray)

MsgBox($MB_SYSTEMMODAL, "Average", $nAverage)

Func _Average($aArray)

    $iCount = UBound($aArray)

    $iTotal = 0

    For $i = 0 To $iCount - 1
        $iTotal += $aArray[$i]
    Next

    Return $iTotal / $iCount

EndFunc

Please ask if you have any questions.

M23

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see my UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Link to comment
Share on other sites

i include your script to my script

why the output is 0?

#include <ButtonConstants.au3>
#include <EditConstants.au3>
#include <GUIConstantsEx.au3>
#include <StaticConstants.au3>
#include <WindowsConstants.au3>
#include <Array.au3>
#include <IE.au3>
#Region ### START Koda GUI section ### Form=F:\data2\tes\array.kxf
$Form1 = GUICreate("Form1", 258, 155, 192, 124)
$Group1 = GUICtrlCreateGroup("", 8, 1, 241, 145)
$numberarray = GUICtrlCreateInput("10", 80, 14, 73, 21)
$Label1 = GUICtrlCreateLabel("Input Array", 16, 16, 55, 17)
$Edit1 = GUICtrlCreateEdit("", 17, 40, 217, 97)
$Button1 = GUICtrlCreateButton("RUN", 160, 11, 75, 25)
GUICtrlCreateGroup("", -99, -99, 1, 1)
GUISetState(@SW_SHOW)
#EndRegion ### END Koda GUI section ###
Global $aspot[999], $savespot, $sstring, $sstring1[9999], $span, $idx = 1
While 1
    $nMsg = GUIGetMsg()
    Switch $nMsg
    case $button1
       runarray()
        Case $GUI_EVENT_CLOSE
            Exit

    EndSwitch
WEnd

func runarray()
    If @error Then Return False
    Local $oie
    $url = "https://www.binary.com/trading?l=EN"
        If NOT WinExists("Binary.com") Then
        $oie = _IECreate($url)
        Sleep(50)
    Else
        $oie = _IEAttach("Binary.com", "instance", 1)
        ;WinActivate("Binary.com")
        Sleep(50)
     EndIf
     while 1
     $span = _iegetobjbyid($oie, "spot")
        If NOT IsObj($span) Then ContinueLoop
        If $savespot = $span.innerhtml Then ContinueLoop
         GUICtrlSetData($edit1, "tick " & $idx & " " & "adalah " & $Span.innerHTML & @CRLF, 1)
        $aspot[$idx] = $span.innerhtml
        $savespot = $span.innerhtml
        $idx += 1


;_ArrayDisplay($aArray, "Array", Default, 8)
if $idx > 6 then
   Global $aArray[5]
$nAverage = _Average($aArray)

;MsgBox($MB_SYSTEMMODAL, "Average", $nAverage)
guictrlsetdata($edit1, $nAverage & @CRLF, 1)
EndIf
WEnd
   EndFunc
Func _Average($aArray)

    $iCount = UBound($aArray)

    $iTotal = 0

    For $i = 0 To $iCount - 1
        $iTotal += $aArray[$i]
    Next

    Return $iTotal / $iCount

EndFunc

 

Link to comment
Share on other sites

  • Moderators

mendrofa,

You declare the array but do not set any values to the elements - and the average of 5 zeroes is, unsurprisingly, zero.

M23

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see my UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Link to comment
Share on other sites

  • Moderators

mendrofa,

Neither do I understand what you are trying to do. If you want to average the last 5 "spot" (whatever that means) then you need to extract the value of each "spot" from the website and enter them into the array - then you can average them.

But from what I can see you are dabbling in financial markets and so I have no interest in helping you further - sorry.

M23

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see my UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Link to comment
Share on other sites

7 hours ago, mendrofa said:

i want make, it get 5 last spot or regardless  i input
how to make like that sir

iam so so not understand

Ask a young child what''s the average age of a town with zero population.

So is whith the average your array, It's declared but never populated.

I not understand you are wondering about that average is zero, you never do a input (assigning data to array). So just do that and after you get a result from func _average. 

Link to comment
Share on other sites

  • 2 weeks later...

Hi Mendrofa,

A:

the script you gave as a BMP example is doing calculations to 1.) fill the array, 2.) to calculate something called weightedArray.

No clue at all, what you finally want to achive. So my suggestion is to describe your *PROBLEM*, instead of asking to transfer a given *SOLUTION* to autoit.

 

B:

Arrays can be hard to handle and to understand, if you never worked with them. Some people might have forgotten about this fact ;-)

Autoit Arrays start to count the elements at 0 (zero). In Autoit there are two general "flavous" to use array: Element [0] is holding the number of elements. Several functions return by default such arrays. Try this script:

#include <array.au3>

$StringToBeSplit="1,2,3,5,7,9,100"
$aSplit=StringSplit($StringToBeSplit,",")
_ArrayDisplay($aSplit) ;Note: The Element 0 has the Value of 7 = "count of elements"

Advantage: You can easily loop through the elements for further calculations using this one:

$sum=0

For $i = 1 to $aSplit[0]
    $sum += $aSplit[$i]
Next

$Average=$sum/$aSplit[0] ; Remember: Element [0] is holding the element count

msgbox(0,"Sum",$sum & @CRLF & _
"Average: " & $Average)

Disadvantage: If you change the number of elements in your array, you will always have to take care to adjust the correct array element count in element [0]. Ubound() will always give you the correct array length value, see below.

Or you can use the Array without a "Element Counter" in Element [0]. By this Element [0] is a VALUE itself. Then you need to retrieve the array length using the function ubound($ArrayName).

 

 

Take care: Ubound returns the "real" number of the array's elements, and the fist element is now [0], as autoit arrays start "counting" at "zero" (and not 1):

So if the Array is going from [0] to [6] as in this example, ubound() will give you a 7. For looping though the array, you will have to "fix" this by looping from "0" to "Ubound($aSplit) - 1". (and not from 1 to $array[0])

 

#include <array.au3>

$StringToBeSplit="1,2,3,5,7,9,100"
$aSplit=StringSplit($StringToBeSplit,",",2) ; 2 = don't return element count in [0]
_ArrayDisplay($aSplit) ; Now [0] is a Array Element, not the element count!

$sum=0

For $i = 0 to UBound($aSplit) - 1
    $sum+= $aSplit[$i]
Next

$Average=$sum/UBound($aSplit)  ; Remember: Element [0] is holding an element, So ubound gives you the count of elements

msgbox(0,"Sum",$sum & @CRLF & _
"Average: " & $Average)

 

If you loop from 1 to Ubound($aSplit), you will

  1. drop the 1st array element
  2. receive an Error:
    Array variable has incorrect number of subscripts or subscript dimension range exceeded.

... as you tried to address element $aSplit[7], which basically doesn't exist.

 

Regards, Rudi.

Edited by rudi

Earth is flat, pigs can fly, and Nuclear Power is SAFE!

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...