Jump to content

array problem


Recommended Posts

i'm trying to build a function that queries the database then returns an array of results

here is the relavant code:

$i = 1

            While $adoRsBan.eof = False
                $auths[$i] = StringStripCR($adoRsBan.Fields.Item("description").Value)
                $adoRsBan.MoveNext()
                $i += 1
            wend

            return($auths)

but it throws Expected a "=" operator in assignment statement.:

ive tried to DIM the array and changing to 0 index but i get various errors about the format plus i dont know how big to make it since it appears .RecordCount is not supported in autoit. how can i dynamically assign arrays?

Link to comment
Share on other sites

AutoIt supports the COM Object interface used for ADO. Your ADO options (specifically the Cursor Type of your record set) may not allow .RecordCount for that particular query. Ref: W3Schools ADO Tutorial

Note: This property will return -1 for a forward-only cursor; the actual count for a static or keyset cursor; and -1 or the actual count for a dynamic cursor.

It's possible the nested COM property is messing with it though. Try saving the value to a variable first:
$i = 1

            While $adoRsBan.eof = False
                $sVal = $adoRsBan.Fields.Item("description").Value
                $auths[$i] = StringStripCR($sVal)
                $adoRsBan.MoveNext()
                $i += 1
            wend

;)

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

AutoIt supports the COM Object interface used for ADO. Your ADO options (specifically the Cursor Type of your record set) may not allow .RecordCount for that particular query. Ref: W3Schools ADO Tutorial

yeah i get a -1 so declaring the array with recordcount is out. but thats fine there should be a way to initilize an empty array then fill it per loop iteration without the need for the total members with recordcount.

now i can use this code and it kinda does that

local $auths[1]

            While $adoRsBan.eof = False
                _ArrayAdd($auths, StringStripCR($adoRsBan.Fields.Item("description").Value))
                $adoRsBan.MoveNext()

            wend
            return($auths)

problem is the array's first element is empty and arrayadd sticks it on the end. i could kludge around this with a counter and some logic but it seems like there should be a better solution

Link to comment
Share on other sites

Check out the ReDim command, it will allow you to dynamically change the size of the array without deleting the contents of the array.

#include <array.au3>
Local $Array[2]
For $i = 0 To 100
    $Array[$i] = $i
    ReDim $Array[$i + 2]
Next
_ArrayDisplay($Array)

If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

Link to comment
Share on other sites

Check out the ReDim command, it will allow you to dynamically change the size of the array without deleting the contents of the array.

#include <array.au3>
Local $Array[2]
For $i = 0 To 100
    $Array[$i] = $i
    ReDim $Array[$i + 2]
Next
_ArrayDisplay($Array)

hmm i had used redim before in a nother language forgot to check if autoit supported it.

still a bit tricky witht he loop i have. since i would still have to initilize it with at least one slot if my search returns only one query (which wily be common) and i redim within the loop ill end up with a dead cell at the end.

lacking an elegant solution i went with this :

local $auths[1]

            While $adoRsBan.eof = False
                _ArrayAdd($auths, StringStripCR($adoRsBan.Fields.Item("description").Value))
                $adoRsBan.MoveNext()

            wend
            _ArrayDelete($auths, 0)

            return($auths)

dim it with a blank space in [0] then use array add to tack on all the crap i need then delete the first element before returning it.

Link to comment
Share on other sites

I'd suggest one of the following:

This one starts writing from element 0 and should be a little faster than your method.

local $auths[1], $iUbound = 1
While $adoRsBan.eof = False
    ReDim $auths[$iUbound]
    $auths[$iUbound -1] = StringStripCR($adoRsBan.Fields.Item("description").Value)
    $adoRsBan.MoveNext()
    $iUbound += 1
WEnd
Return $auths

While this one will only do one Redim, because Redims can take a long time if you do allot of them, meaning it's fast, but a bit messy.

local $auths[160777216], $iUbound = 0 ;16777216 being the maximum array size in autoit. Make a reasonable estimate for yourself as a large array will eat memory until it's schrunk.
While $adoRsBan.eof = False
    $auths[$iUbound -1] = StringStripCR($adoRsBan.Fields.Item("description").Value)
    $adoRsBan.MoveNext()
    $iUbound += 1
WEnd
ReDim $auths[$iUbound]
Return $auths
Link to comment
Share on other sites

I'd suggest one of the following:

This one starts writing from element 0 and should be a little faster than your method.

local $auths[1], $iUbound = 1
While $adoRsBan.eof = False
    ReDim $auths[$iUbound]
    $auths[$iUbound -1] = StringStripCR($adoRsBan.Fields.Item("description").Value)
    $adoRsBan.MoveNext()
    $iUbound += 1
WEnd
Return $auths

While this one will only do one Redim, because Redims can take a long time if you do allot of them, meaning it's fast, but a bit messy.

local $auths[160777216], $iUbound = 0 ;16777216 being the maximum array size in autoit. Make a reasonable estimate for yourself as a large array will eat memory until it's schrunk.
While $adoRsBan.eof = False
    $auths[$iUbound -1] = StringStripCR($adoRsBan.Fields.Item("description").Value)
    $adoRsBan.MoveNext()
    $iUbound += 1
WEnd
ReDim $auths[$iUbound]
Return $auths

heh interesting way of looking at it. since most of the stuff is small scale performance isn't a huge issue so ill just stick to what i have right now. if performance was a big deal i would probably run a count loop and dim once with the proper size or possibly add a count(*) to my sql query and use that value to assume how many records i was getting back.

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