Sign in to follow this  
Followers 0
MarkRobbins

Bug In AssocArrays.au3

9 posts in this topic

Somewhere out there is a UDF lib <AssocArrays.au3>

Autoit v3.3.8.1

Unless I am doing something really stupid, it has a bug. Here is the repro, does anyone have a fix?

I've attached the lib, it doesn't show a version number.

#include <AssocArrays.au3>
#include <ButtonConstants.au3>

Global $consts
    AssocArrayCreate($consts,1)
    AssocArrayAssign($consts,'Left',$BS_LEFT)
    AssocArrayAssign($consts,'Right',$BS_RIGHT)
    AssocArrayAssign($consts,'Bottom',$BS_BOTTOM)
    AssocArrayAssign($consts,'Center',$BS_CENTER)
    AssocArrayAssign($consts,'DefPushButton',$BS_DEFPUSHBUTTON)
    AssocArrayAssign($consts,'MultiLine',$BS_MULTILINE)
    AssocArrayAssign($consts,'Top',$BS_TOP)
    AssocArrayAssign($consts,'VCenter',$BS_VCENTER)
    AssocArrayAssign($consts,'Icon',$BS_ICON)
    AssocArrayAssign($consts,'Bitmap',$BS_BITMAP)
    AssocArrayAssign($consts,'Flat',$BS_FLAT)
    AssocArrayAssign($consts,'Notify',$BS_NOTIFY); NOTE: Repeating this line will make it exist

Inspect('Notify'); Says not exist, unexpected!
Inspect('Flat');Exists
Inspect('Top');Exists

Func Inspect($test)
  If Not AssocArrayExists($consts,$test) Then
    MsgBox(0,$test,'Does not exist')
  Else
    MsgBox(0,$test,'Does exist')
  EndIf
EndFunc

AssocArrays.au3

Share this post


Link to post
Share on other sites



Hi, MarkRobbins. Normally I suggest posting in the UDF's original topic, so the creator can assist directly. I see that Nutster has not been active in a while, though.


√-1 2^3 ∑ π, and it was delicious!

Share this post


Link to post
Share on other sites

MarkRobbins,

I can see a possible reason - somewhere in the original thread it is suggested that you create the AssocArray with a prime number of elements sufficiently large to cover the expected number of items. I seem to remember that it has to do with collisons when the values produce similar hashes - but do not take my word for it. :wacko:

Anyway, if I create the array with a suitable prime number of elements (I tried 19 and 29), all the keys respond "Does exist". :thumbsup:

Does the same happen for you when you try. :huh:

M23


Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind._______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

 

Share this post


Link to post
Share on other sites

MarkRobbins,

I can see a possible reason - somewhere in the original thread it is suggested that you create the AssocArray with a prime number of elements sufficiently large to cover the expected number of items. I seem to remember that it has to do with collisons when the values produce similar hashes - but do not take my word for it. :wacko:

Anyway, if I create the array with a suitable prime number of elements (I tried 19 and 29), all the keys respond "Does exist". :thumbsup:

Does the same happen for you when you try. :huh:

M23

 

"1" is prime o:)

I've moved on to an alternate method, since I didn't want to build on shaky ground. Check this out:

#include <AssocArrays.au3>
#include <Array.au3>

Global $oError = ObjEvent("AutoIt.Error", "_ErrFunc")
Global $_ComObj_proxy
Global $_com
Global $_Js
JScriptInit()



;Create javascript COM objects
Global $ob = $_com.new_object("")
Global $ob2 = $_com.new_object("")



;Msg(VarGetType($ob.keys()));Object
;Msg($ob.keys().item(0));""
;Msg($ob.keys().length);0

;set a single value
$ob.set('v','a var')

;set many values
$ob.set2('one',1,'two',2)

;check if exists examples
#cs
Msg($ob.has('one'));true
Msg(VarGetType($ob.get('two')));Int32
#ce

;iterate values example

Global $x,$end=$ob.keys().length-1
Global $k=$ob.keys()
For $x=0 To $end
  Msg($k.item($x)&':'&$ob.get($k.item($x)))
Next

;store an array as a value example
#cs
Global $arr[3]=[1,2,3]
$ob2.set('arr',$arr)
Local $a=$ob2.get('arr');have to put in a var first, else error if inline
_ArrayDisplay($a)
#ce

; test and compare
Global $timer = TimerInit()
;many items
$end=1000
;insert
For $x=0 to $end
   $ob.set("val"&$x,"value"&$x)
Next

;read
Local $dum
For $x=0 to $end
   $dum=$ob.get("val"&$x)
Next
; keep time
Global $t1=TimerDiff($timer)


;compare to AssocArray
Global $aa
AssocArrayCreate($aa,1000)

;insert
$timer = TimerInit()
For $x=0 to $end
  AssocArrayAssign($aa,"val"&$x,"value"&$x)
Next

;read
For $x=0 to $end
  $dum=AssocArrayGet($aa,"val"&$x)
Next
;keep time
Global $t2=TimerDiff($timer)
;report
MsgBox(0,'insert,read of items:'&$end,'Millisecs, javascript:'&Int($t1)&@CRLF&'AssocArray:'&Int($t2))
;
;random access one item
$timer = TimerInit()
;by indexing
$ob.get("val500")
Global $t3=TimerDiff($timer)


$timer = TimerInit()
;directly!!
Local $v=$ob.val500
Global $t31=TimerDiff($timer)

; assoc
$timer = TimerInit()
AssocArrayGet($aa,"val500")
Global $t4=TimerDiff($timer)

;report
MsgBox(0,'random access,1 item of '&$end,'Millisecs, javascript by indexing:'&$t3&@CRLF&'javscript direct:'&$t31&@CRLF&' AssocArray:'&$t4)
Exit



Func _ComObj_init ($VBScriptSupport = false)
  Local $_Script
  $_Script = ObjCreate("ScriptControl")
  $_Script.Language = "JScript"
  $_Script.Timeout = 60000
  ;$_Script.AddCode("var $=this,arrays=[],vb;function set_vbscript($) {vb=$;vb.Language='VBScript';vb.AllowUI=true;}function alert(s){if(vb)vb.Eval('MsgBox(Unescape(""'+s+'""), 0, ""Autoit"")')}function get_proxy(){return $}function new_object($){$=new Function('return{'+$+'}')();$.set=function(k, v){$[k]=v};$.unset=function(k){delete $[k]};$.set_object=function(k,$){this.set(k,new_object($))};$.set_array=function(){var v=[];for(var i=0;i<arguments.length;i++)v[i]=arguments[i];$.set(v.shift(),new_array.apply(this,v))};$.set_function=function(k,p,$){this.set(k,new_function(p,$))};return $}function new_array(){var v=[];for(var i=0;i<arguments.length;i++)v[i]=arguments[i];return v}function array_get($,k){return $[k]}function array_set($,k,v){return $[k]=v}var new_function=(function(){function F(r) {return Function.apply(this,r)}F.prototype = Function.prototype;return function(p,$){p=p.split(/\s*,\s*/);p.push($);return new F(p)}})()")
  ;a[a.length]=i;
  ;function new_array(){var v=[];for(var i=0;i<arguments.length;i++)v[i]=arguments[i];return v}
  Local $more= _
  "$.get=function(k){return $[k]};" & _
  "$.has=function(n){" & _
  "  for (var i in this){" & _
  "    if (i!=n){" & _
  "      continue;" & _
  "    }" & _
  "    if (!this.hasOwnProperty(i)){" & _
  "      continue;" & _
  "    }" & _
  "    if (typeof(this[i])=='function'){" & _
  "      continue;" & _
  "    }" & _
  "    return true;" & _
  "  };" & _
  "  return false;" & _
  "};" & _
  "$.keys=function(){" & _
  "  var a=[];" & _
  "  for (var i in this){" & _
  "    if (this.hasOwnProperty(i)){" & _
  "      if (typeof(this[i])!='function'){" & _
  "        a[a.length]=i;" & _
  "      }" & _
  "    }" & _
  "  };" & _
  "  return a;" & _
  "};" & _
  "$.set2=function(){" & _
  "  for(var x=0;x<arguments.length-1;x+=2){" & _
  "    this[arguments[x]]=arguments[x+1]" & _
  "  }" & _
  "};" & _
  "Array.prototype.item=function(n){" & _
  "  return this[n];" & _
  "};" & _
  "Array.prototype.asString=function(d){" & _
  "  var s='';" & _
  "  for(x=0;x<this.length;x++){" & _
  "    if(s==''){s=this[x];}else{s+=d+this[x];}" & _
  "  }" & _
  "  return s;" & _
  "};"
  $_Script.AddCode("var $=this,arrays=[],vb;function set_vbscript($) {vb=$;vb.Language='VBScript';vb.AllowUI=true;}function alert(s){if(vb)vb.Eval('MsgBox(Unescape(""'+s+'""), 0, ""Autoit"")')}function get_proxy(){return $}function new_object($){$=new Function('return{'+$+'}')();$.set=function(k, v){$[k]=v};"&$more&"$.unset=function(k){delete $[k]};$.set_object=function(k,$){this.set(k,new_object($))};$.set_array=function(){var v=[];for(var i=0;i<arguments.length;i++)v[i]=arguments[i];$.set(v.shift(),new_array.apply(this,v))};$.set_function=function(k,p,$){this.set(k,new_function(p,$))};return $}function new_array(){var v=[];for(var i=0;i<arguments.length;i++)v[i]=arguments[i];return v}function array_get($,k){return $[k]}function array_set($,k,v){return $[k]=v}var new_function=(function(){function F(r) {return Function.apply(this,r)}F.prototype = Function.prototype;return function(p,$){p=p.split(/\s*,\s*/);p.push($);return new F(p)}})()")
  If $VBScriptSupport = true Then $_Script.Run("set_vbscript", ObjCreate("ScriptControl"))
  Return $_Script
EndFunc

Func JScriptInit()
  $_ComObj_proxy = _ComObj_init(true)
  $_com = $_ComObj_proxy.Run("get_proxy")
  $_Js = $_com.new_object("")
  ;$_Js.set_function("AIOSetProperty","o,n,v","o[n]=v;")
EndFunc


Func _ErrFunc()
    ;ConsoleWrite("COM Error, ScriptLine(" & $oError.scriptline & ") : Number 0x" & Hex($oError.number, 8) & " - " & $oError.windescription & @CRLF)
  Local $line="[COM Error]:s"&$oError.source&" ScriptLine(" & $oError.scriptline & ") : Number 0x" & Hex($oError.number, 8) & " - " & $oError.windescription
  Local $line2="" &$oError.source &':'& $oError.scriptline & ":" & $oError.windescription
  ;logerr($line)
  ;Msg($line2)
  exit
EndFunc

Func Msg($s)
  MsgBox(0,'',$s)
EndFunc

What do you think?

Share this post


Link to post
Share on other sites

There is Scripting.Dictionary you can use.


_AdapterConnections()_AlwaysRun()_AppMon()_AppMonEx()_BinaryBin()_CheckMsgBox()_CmdLineRaw()_ContextMenu()_ConvertLHWebColor()/_ConvertSHWebColor()_DesktopDimensions()_DisplayPassword()_DotNet_Load()/_DotNet_Unload()_Fibonacci()_FileCompare()_FileCompareContents()_FileNameByHandle()_FilePrefix/SRE()_FindInFile()_GetBackgroundColor()/_SetBackgroundColor()_GetConrolID()_GetCtrlClass()_GetDirectoryFormat()_GetDriveMediaType()_GetFilename()/_GetFilenameExt()_GetHardwareID()_GetIP()_GetIP_Country()_GetOSLanguage()_GetSavedSource()_GetStringSize()_GetSystemPaths()_GetURLImage()_GIFImage()_GoogleWeather()_GUICtrlCreateGroup()_GUICtrlListBox_CreateArray()_GUICtrlListView_CreateArray()_GUICtrlListView_SaveCSV()_GUICtrlListView_SaveHTML()_GUICtrlListView_SaveTxt()_GUICtrlListView_SaveXML()_GUICtrlMenu_Recent()_GUICtrlMenu_SetItemImage()_GUICtrlTreeView_CreateArray()_GUIDisable()_GUIImageList_SetIconFromHandle()_GUIRegisterMsg()_GUISetIcon()_Icon_Clear()/_Icon_Set()_IdleTime()_InetGet()_InetGetGUI()_InetGetProgress()_IPDetails()_IsFileOlder()_IsGUID()_IsHex()_IsPalindrome()_IsRegKey()_IsStringRegExp()_IsSystemDrive()_IsUPX()_IsValidType()_IsWebColor()_Language()_Log()_MicrosoftInternetConnectivity()_MSDNDataType()_PathFull/GetRelative/Split()_PathSplitEx()_PrintFromArray()_ProgressSetMarquee()_ReDim()_RockPaperScissors()/_RockPaperScissorsLizardSpock()_ScrollingCredits_SelfDelete()_SelfRename()_SelfUpdate()_SendTo()_ShellAll()_ShellFile()_ShellFolder()_SingletonHWID()_SingletonPID()_Startup()_StringCompact()_StringIsValid()_StringRegExpMetaCharacters()_StringReplaceWholeWord()_StringStripChars()_Temperature()_TrialPeriod()_UKToUSDate()/_USToUKDate()_WinAPI_Create_CTL_CODE()_WinAPI_CreateGUID()_WMIDateStringToDate()/_DateToWMIDateString()Au3 script parsingAutoIt SearchAutoIt3 PortableAutoIt3WrapperToPragmaAutoItWinGetTitle()/AutoItWinSetTitle()CodingDirToHTML5FileInstallrFileReadLastChars()GeoIP databaseGUI - Only Close ButtonGUI ExamplesGUICtrlDeleteImage()GUICtrlGetBkColor()GUICtrlGetStyle()GUIEventsGUIGetBkColor()Int_Parse() & Int_TryParse()IsISBN()LockFile()Mapping CtrlIDsOOP in AutoItParseHeadersToSciTE()PasswordValidPasteBinPosts Per DayPreExpandProtect GlobalsQueue()Resource UpdateResourcesExSciTE JumpSettings INISHELLHOOKShunting-YardSignature CreatorStack()Stopwatch()StringAddLF()/StringStripLF()StringEOLToCRLF()VSCROLLWM_COPYDATAMore Examples...

Updated: 04/09/2015

Share this post


Link to post
Share on other sites

MarkRobbins,

 

"1" is prime

But what I said was:

create the AssocArray with a prime number of elements sufficiently large to cover the expected number of items

and I do not think "1" qualifies on that count (pun fully intended). ;)

As I explained, when I used a suitably sized prime in your code to create the array everything worked as expected. And I have been using this UDF for many years without problem, so I do not think it is too "shaky". :)

M23


Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind._______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

 

Share this post


Link to post
Share on other sites

For the record, 1 is not a prime number by any valid known definition of "prime".

Anyway, a scripting dictionary would work irrespective of the number of entries it contains.


This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.
Another excellent RegExp tutorial. Don't forget downloading your copy of up-to-date pcretest.exe and pcregrep.exe here
RegExp tutorial: enough to get started
PCRE v8.33 regexp documentation latest available release and currently implemented in AutoIt beta.

SQLitespeed is another feature-rich premier SQLite manager (includes import/export). Well worth a try.
SQLite Expert (freeware Personal Edition or payware Pro version) is a very useful SQLite database manager.
An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious work.
SQL tutorial (covers "generic" SQL, but most of it applies to SQLite as well)
A work-in-progress SQLite3 tutorial. Don't miss other LxyzTHW pages!
SQLite official website with full documentation (may be newer than the SQLite library that comes standard with AutoIt)

Share this post


Link to post
Share on other sites

I lol'd at 1 being a prime number. Very definition of a prime number is: a natural number greater than 1 that has no positive divisors other than 1 and itself. :)


√-1 2^3 ∑ π, and it was delicious!

Share this post


Link to post
Share on other sites

Yup, that or

p prime ⇔ τ(p) = 2, where τ (the greek letter tau) represents the number of positive divisors of p.


This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.
Another excellent RegExp tutorial. Don't forget downloading your copy of up-to-date pcretest.exe and pcregrep.exe here
RegExp tutorial: enough to get started
PCRE v8.33 regexp documentation latest available release and currently implemented in AutoIt beta.

SQLitespeed is another feature-rich premier SQLite manager (includes import/export). Well worth a try.
SQLite Expert (freeware Personal Edition or payware Pro version) is a very useful SQLite database manager.
An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious work.
SQL tutorial (covers "generic" SQL, but most of it applies to SQLite as well)
A work-in-progress SQLite3 tutorial. Don't miss other LxyzTHW pages!
SQLite official website with full documentation (may be newer than the SQLite library that comes standard with AutoIt)

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