Sign in to follow this  
Followers 0
which1

Local vs Global scope for Array variable

10 posts in this topic

Hi everybody,

In the following code, declaring $arKeyWords_V[1][2] as Local works fine, while declaring it as Global gives an error : Subscript used with non-Array variable.

-1 Could someone be so kind to explain me why.

-2 By the way, I need $arKeyWords_V[1][2] in other parts (and files) of the programm.

;Global $arKeyWords_V[1][2]

Func _populate_vesale()

Local $sData_V, $arKeyWords_V, $hQuery, $aRow_V, $j = 0

Local $arKeyWords_V[1][2]

_SQLite_Query($hVesaleDb, "SELECT medecin_vesale_id , medecin_vesale_nom || ' ' || medecin_vesale_prenom FROM medecins_vesale ORDER BY medecin_vesale_nom;", $hQuery)

While _SQLite_FetchData($hQuery, $aRow_V) = $SQLITE_OK

$arKeyWords_V[$j][0] = $aRow_V[0] ; medecin_id

$arKeyWords_V[$j][1] = $aRow_V[1] ; Nom-Prénom-ville

ReDim $arKeyWords_V[uBound($arKeyWords_V,1)+1][2]

$j += 1

WEnd

; ../...

EndFunc

Share this post


Link to post
Share on other sites



#2 ·  Posted (edited)

which1,

Can you copy the exact error and post it? Post all of the console output so we can see what line it is pointing at.

kylomas

edit: also, are you sure that you are getting data from the fetchdata? I believe that $a_Row_V will only become an array if there is data returned. If I remember correctly, $SQLITE_OK just means that the function ran without error, whether or not data was returned.

Edited by kylomas

Forum Rules         Procedure for posting code

"I like pigs.  Dogs look up to us.  Cats look down on us.  Pigs treat us as equals."

- Sir Winston Churchill

Share this post


Link to post
Share on other sites

When you declare it Global and not local, your first line in the function redeclares the variable name as a simple variable instead of an array.

Local $sData_V, $arKeyWords_V, $hQuery, $aRow_V, $j = 0

You then redeclare the variable as an array in the next line so it works ok.

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

Share this post


Link to post
Share on other sites

#4 ·  Posted (edited)

@BremanNH - yes, I was looking at that. He then declares the same variable as an array within the func. Not the best practice but does'nt seem like it should be causing the error he is reporting.

I tested that with this

;
;
;
#include <array.au3>
local $a
local $a[1]
$a[0] = 'array element'
_arraydisplay($a)
and it worked.

kylomas

edit: additional info

This pretty much mimics what the OP is doing with the variable in question

#include <array.au3>

global $a[1]

_1()

func _1()

    local $a
    local $a[1]

    $a[0] = 'array element'

    _arraydisplay($a)

endfunc

and it runs OK

Edited by kylomas

Forum Rules         Procedure for posting code

"I like pigs.  Dogs look up to us.  Cats look down on us.  Pigs treat us as equals."

- Sir Winston Churchill

Share this post


Link to post
Share on other sites

#5 ·  Posted (edited)

Thanks to both of you !

The error indeed was due to $arKeyWords_V . Solved now :

Global $arKeyWords_V[1][2]

Func _populate_vesale()

Local $sData_V, $hQuery, $aRow_V, $j = 0 ; ---> $arKeyWords_V

_SQLite_Query($hVesaleDb, "SELECT medecin_vesale_id , medecin_vesale_nom || ' ' || medecin_vesale_prenom FROM medecins_vesale ORDER BY medecin_vesale_nom;", $hQuery)

While _SQLite_FetchData($hQuery, $aRow_V) = $SQLITE_OK

$arKeyWords_V[$j][0] = $aRow_V[0] ; medecin_id

$arKeyWords_V[$j][1] = $aRow_V[1] ; Nom-Prénom-ville

ReDim $arKeyWords_V[uBound($arKeyWords_V,1)+1][2]

$j += 1

WEnd

; ../...

EndFunc

@kylomas : You'r right ! Next time i'll try not to forget the console output.

Edit :

The problem is now : why does the code work proposed by kylomas actually work ?

#include <array.au3>

global $a[1]

_1()

func _1()

; local $a

local $a[1]

local $a

$a[0] = 'array element'

_arraydisplay($a)

endfunc

Edited by which1

Share this post


Link to post
Share on other sites

Please use [autoit][/autoit] tags when posting AutoIt code. Thanks.


_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

@BremanNH - yes, I was looking at that. He then declares the same variable as an array within the func. Not the best practice but does'nt seem like it should be causing the error he is reporting.

I was assuming that when he declared it Global and it failed that he wasn't using the Local declaration of the array at that time.

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

Share this post


Link to post
Share on other sites

@which1

You would have better time (and not only simpler but also more robust code) using _SQLiteGetTable2d instead of *_Query and loop on *_FetchData.


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

@BrewmanNH - yes, bit of a moving target.


Forum Rules         Procedure for posting code

"I like pigs.  Dogs look up to us.  Cats look down on us.  Pigs treat us as equals."

- Sir Winston Churchill

Share this post


Link to post
Share on other sites

@jchd : thanks for the hint. Much easyer indeed. Adopted !

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