Sign in to follow this  
Followers 0
Phaser

Arrays again

20 posts in this topic

Hi Everyone

I am using a do statement to loop through an array data set x amount of times, from the array result if something is flagged I want to push it to a new array, the results could be from 1 to 25 or even 30 so the size of the new array needs to be dyanimcly created/adjusted, how do I set this up? I have tried

Local $newarray[$size]

$i = 0
Do
$checking = _ArraySearch($list, $i,0,40,0,0,1,0)
            
If $checking = -1 Then
_ArrayPush($newarray,$i,1)
$size = $size+1
EndIf
        
$i = $i+1
Until $i = 40

_ArrayDisplay($newarray)

Any help in the correct syntax would be appreciated

Share this post


Link to post
Share on other sites



#2 ·  Posted (edited)

UBound is what you want

Do
Until $i = UBound($Array_Name) -1

However, I use it this way

For $x = 1 to UBound($Array) -1
    ; test array here
Next

8)

Edited by Valuater

NEWHeader1.png

Share this post


Link to post
Share on other sites

Thnaks for the fast reply Valuater but I have not used Ubound before so am unsure of the makeup invlolved, if you could just correct the below I should be able to work with it

Local $newarray[$size]

$i = 0
Do
$checking = _ArraySearch($list, $i,0,40,0,0,1,0)
            
If $checking = -1 Then
_ArrayPush($newarray,$i,1)
$size = $size+1
EndIf
        
$i = $i+1
Until $i = 40 <---------------does Ubound replace this

_ArrayDisplay($newarray)

Share this post


Link to post
Share on other sites

#4 ·  Posted (edited)

Thnaks for the fast reply Valuater but I have not used Ubound before so am unsure of the makeup invlolved, if you could just correct the below I should be able to work with it

Local $newarray[$size]

$i = 0
Do
$checking = _ArraySearch($list, $i,0,40,0,0,1,0)
            
If $checking = -1 Then
_ArrayPush($newarray,$i,1)
$size = $size+1
EndIf
        
$i = $i+1
Until $i = 40 <---------------does Ubound replace this

_ArrayDisplay($newarray)

Yeah.

UBound($Array_Name) -1
Replaces

40

But like he said:

For $x = 1 to UBound($Array) -1
    ; test array here
Next

Should probably be the loop to use for this particular function. Looks nicer and it's a bit easier to understand.

Edited by jebus495

Share this post


Link to post
Share on other sites

#5 ·  Posted (edited)

Well it doesn't work I get an error here

Local $newarray[$size]

Local $newarray[^ ERROR

EDIT

And, the 40 cant be changed as its the figure I use the create what to search the array for, the array I am attempting to dynamicly populate is this bit

If $checking = -1 Then

_ArrayPush($newarray,$i,1)

$size = $size+1

EndIf

Please advise

Edited by Phaser

Share this post


Link to post
Share on other sites

Well it doesn't work I get an error here

Local $newarray[$size]

Local $newarray[^ ERROR

EDIT

And, the 40 cant be changed as its the figure I use the create what to search the array for, the array I am attempting to dynamicly populate is this bit

If $checking = -1 Then

_ArrayPush($newarray,$i,1)

$size = $size+1

EndIf

Please advise

And where are you setting $size to an integer value before using it?

:)


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

this kind of dynamic array is suited more to a counter checked array. In other words, you keep the first element as the number of the next available index into the array and redim the array as necessary. For example:

Local $newarray[20] = [1] ; Will be reallocated as necessary.

For $i = 0 To UBound($list)-1
   If _ArraySearch($list, $i,0,40,0,0,1,0) = -1 Then
     If $newarray[0] >= UBound($newarray) Then Redim $newarray[$newarray[0]+10] ; + 10 elements to avoid redimming over and over which is slower.
     $newarray[$newarray[0]] = $i
     $newarray[0] += 1
   EndIf
Next

; Redim to the number of elements in the array.
Redim $newarray[$newarray[0]-1]

; Now reverse the array because you've appended elements instead of pushing them.

I hope it's correct because it's not tested.

PS. PsaltyDS is right. AutoIt should throw a warning that $size is used but never assigned a value.

Share this post


Link to post
Share on other sites

PS. PsaltyDS is right. AU3Check should throw a warning that $size is used but never assigned a value.

:)

Nice to see you btw, you've been away for a while :)


Broken link? PM me and I'll send you the file!

Share this post


Link to post
Share on other sites

Thanks for the replies people, there is a bit of a misunderstanding, $list is the array I am checking against and the number 40 is the actual values I am looking for within $list

Local $newarray[20] = [1] ; Will be reallocated as necessary.

For $i = 0 To 40
   If _ArraySearch($list, $i,0,40,0,0,1,0) = -1 Then
     If $newarray[0] >= UBound($newarray) Then Redim $newarray[$newarray[0]+10] ; + 10 elements to avoid redimming over and over which is slower.
     $newarray[$newarray[0]] = $i
     $newarray[0] += 1
   EndIf
Next

; Redim to the number of elements in the array.
Redim $newarray[$newarray[0]-1]

It almost works perfectly BUT I am getting one duplicate entry when I display the array always at [0]

As I am looping through from 0 to 40 to see if that value is in $list, if not in add to newarray 0 to 40 is only checking each value once so how does it end up with a repeat as I said at the first position of the redim array [0]? how do I stop this?

Share this post


Link to post
Share on other sites

It's the array counter. It's used in the loop as you can see. Use _ArrayDelete() if you want, it'll make _ArrayDisplay prettier but it has a good context in consequence loops:

For $i = 1 To $newarray[0]
   ; Work with array.
Next

If it's not the behavior you want, use _ArrayDelete().

Share this post


Link to post
Share on other sites

thanks Authenticity I have used _ArrayDelete()

I am now using the values from the newarray to create a controlclick command, I have an include file with the x/y positions like this

$39x = 200

$39y = 310

and am trying to use just one controlclick command in a loop, how do I format the variable, for example

$xval = "$" & $newarray[$x] & "x" <------------- Ideally this should produce $39x

$yval = "$" & $newarray[$x] & "y" <------------- Ideally this should produce $39y, they do but are not working when transferred to the controlclick command

ControlClick("WorkForce", "", "", "left", 1, $xval, $yval)

I have the loop and everything working fine, tested with msgbox popup, it's the $sign thats causing problems but needs to be there so it can scan the include for the x/y coords

Share this post


Link to post
Share on other sites

#12 ·  Posted (edited)

In the ControlClick command you don't want the value of $xval and $yval (which is "$39x" and "$39y") but the value of the variables defined by $xval and $yval. So you have to evaluate them. See the Eval function

$xval = Eval("{:content:}quot; & $newarray[$x] & "x")
should give you the value of $s39x in $xval. Edited by water

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2017-04-18 - Version 1.4.8.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (NEW 2017-02-27 - Version 1.3.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2015-04-01 - Version 0.4.0.0) - Download - General Help & Support - Example Scripts
Excel - Example Scripts - Wiki
Word - Wiki
PowerPoint (2015-06-06 - Version 0.0.5.0) - Download - General Help & Support

Tutorials:
ADO - Wiki

 

Share this post


Link to post
Share on other sites

#13 ·  Posted (edited)

thanks for the help water but it gives me an error

Unterminated string.:

$xval = Eval("{:content:}quot; & $newarray[$x] & "x")

I also tried

$xval = Eval("$" & $newarray[$x] & "x")

But that didnt produce a result/click

Edited by Phaser

Share this post


Link to post
Share on other sites

#14 ·  Posted (edited)

My fault. You have to omit the "$" as Eval already knows that a variable is to be used.

$xval = Eval($newarray[$x] & "x")

Edited by water

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2017-04-18 - Version 1.4.8.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (NEW 2017-02-27 - Version 1.3.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2015-04-01 - Version 0.4.0.0) - Download - General Help & Support - Example Scripts
Excel - Example Scripts - Wiki
Word - Wiki
PowerPoint (2015-06-06 - Version 0.0.5.0) - Download - General Help & Support

Tutorials:
ADO - Wiki

 

Share this post


Link to post
Share on other sites

many thanks it now works as expected, cheers water

Share this post


Link to post
Share on other sites

Hit a problem, the array fills fine, everything runs fine after I manipulate the data thats in the array, I now need to empty the array so it can be refilled again with new valyues, whats the best method, I have tried do, while and for with _ArrayDelete like this

$howbig = UBound($newarray) ; get size of array

$j = 0
While $j <= $howbig
    _ArrayDelete($newarray, $j) 

$j = $j+1
WEnd

Its not working, any ideas

Share this post


Link to post
Share on other sites

#17 ·  Posted (edited)

$newarray = 0

Local $newarray[20]
ConsoleWrite(UBound($newarray) & @CRLF)

..for example

Edit: By the way, read the function description of a function before you use it. _ArrayDelete() remove the element from the array so the new array is resized or "" if it's upper bound was 1.

Edited by Authenticity

Share this post


Link to post
Share on other sites

I did read its description, thats why I thought I could just cycle through the array removing each element at the end of the script, I have no idea what you meant with this

$newarray = 0

Local $newarray[20]

ConsoleWrite(UBound($newarray) & @CRLF)

I understand concolewrite, why would I want to do it?

Share this post


Link to post
Share on other sites

Well, if you remove item from an array using _ArrayDelete() then you shouldn't increase the counter or delete the array elements using an inverse loop, i.e:

For $j = UBound($newarray)-1 To 0 Step -1
   ; Delete element
Next

; Or just simple as this loop
For $j = 0 To Ubound($newarray)-1
  _ArrayDelete($newarray, 0) ; Always the first element until $newarray = "" or nothing.
Next

The ConsoleWrite is there for demonstration nothing more. When you set an array variable to 0 it's array attribute is negated but so you can't use ReDim (not tested) but it can be redeclared as one.

Share this post


Link to post
Share on other sites

Another method is to copy the array and loop through the new one but perform the actions on the original.

_ArrayDisplay($aArray)
$aTemp = $aArray
For $i = 0 To Ubound($aTemp)
    If $aTemp[$i] = "" Then _ArrayDelete($aArray, $i)
Next
_ArrayDisplay($aArray)

George

Question about decompiling code? Read the decompiling FAQ and don't bother posting the question in the forums.

Be sure to read and follow the forum rules. -AKA the AutoIt Reading and Comprehension Skills test.***

The PCRE (Regular Expression) ToolKit for AutoIT - (Updated Oct 20, 2011 ver:3.0.1.13) - Please update your current version before filing any bug reports. The installer now includes both 32 and 64 bit versions. No change in version number.

Visit my Blog .. currently not active but it will soon be resplendent with news and views. Also please remove any links you may have to my website. it is soon to be closed and replaced with something else.

"Old age and treachery will always overcome youth and skill!"

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