Parsing arrays

LiveCode is the premier environment for creating multi-platform solutions for all major operating systems - Windows, Mac OS X, Linux, the Web, Server environments and Mobile platforms. Brand new to LiveCode? Welcome!

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller, robinmiller

WebiWan
Posts: 24
Joined: Fri Apr 20, 2018 2:21 am

Re: Parsing arrays

Post by WebiWan » Wed Jan 01, 2020 7:30 pm

dunbarx wrote:
Tue Dec 31, 2019 10:08 pm
Well, we are at least getting somewhere. When you do step through, does the very first line:

Code: Select all

 put myArray[x][1] into tvMake
put anything into tvMake?

...

Craig
No. However, after reading everyone's comments I think we may be circling the problem. Could the "indexing" problem be due to having named keys rather than numbered? In other words, my array looks like this:

1
Database Column Name | Data
2
Database Column Name | Data

Etc.

Should I be addressing the subkeys with the column name?

Happy New Year everyone!

WebiWan
Posts: 24
Joined: Fri Apr 20, 2018 2:21 am

Re: Parsing arrays

Post by WebiWan » Wed Jan 01, 2020 7:34 pm

mrcoollion wrote:
Wed Jan 01, 2020 5:24 pm
Maybe then you can see if there is anything different to the array then you expected that could explain the outcome of your script? This has helped me working with array's.
Regards,

Paul
The only thing I can think of is the subkeys are not numbered, they are named. But I assumed indexing them with a number would work. Like any good array should. ;-)

Happy New Year!

WebiWan
Posts: 24
Joined: Fri Apr 20, 2018 2:21 am

Re: Parsing arrays

Post by WebiWan » Wed Jan 01, 2020 7:48 pm

Someone suggested it would be good to know how the array is created.

Code: Select all

on mouseUp mouseButt
   if mouseButt = 1 then
      if gvConnectID is not a number or gvConnectLite is not a number then
         cmdConnect
      end if
      
      // Go get the last run time
      put the prFile of this stack into tvFile
      put the effective filename of this stack into tvPath
      set the itemDelimiter to slash
      delete last item of tvPath
      put tvPath & slash & tvFile into tvFile
      
      if there is a file tvFile then
         open file tvFile for read
         read from file tvFile until EOF
         put it into tvLastRun
         close file tvFile
      end if
      
      cmdTimestamp tvLastRun  -- This routine simply takes the last run time and turns it into a SQL timestamp
      
      put "Name, GuestMachineName, GuestOperatingSystemName, GuestProcessorName, " &\
            "GuestProcessorVirtualCount, GuestSystemMemoryTotalMegabytes, GuestMachineManufacturerName, "&\
            "GuestMachineModel, GuestMachineProductNumber, GuestMachineSerialNumber" into tvFields
      
      put "SELECT"&& tvFields && "FROM Session WHERE GuestInfoUpdateTime > '" & lvTime & "'" into tvSQL
      put revQueryDatabase (gvConnectLite, tvSQL) into theCursor
      
      if theCursor is an integer then
         cmdCursor2Array theCursor, myArray
      end if
      
      repeat for each key x in myArray
         put myArray[x][1] into tvMake
         put myArray[x][2] into tvModel 
         put myArray[x][3] into tvCompName 
         put myArray[x][4] into tvProdNum
         put myArray[x][5] into tvSerial 
         put myArray[x][6] into tvOSName
         put myArray[x][7] into tvProcessor
         put myArray[x][8] into tvProcessorNum
         put myArray[x][9] into tvRAM
         put myArray[x][10] into tvName
         
And from there it processes each record in the array.

I got the routine cmdCursor2Array from these pages but here it is too:

Code: Select all

command cmdCursor2Array pCursor, @pOutArrayA
   ## Get the names of all the columns in the database cursor
   put revDatabaseColumnNames(pCursor) into theFields
   if theFields begins with "revdberr," then
      put item 2 to -1 of theFields into theError
   end if
   
Thanks everyone. Happy New Year!

bogs
Posts: 5435
Joined: Sat Feb 25, 2017 10:45 pm

Re: Parsing arrays

Post by bogs » Wed Jan 01, 2020 7:56 pm

I know you had a lot of responses and sent a lot of replies, but did you see this response from Axwald, which might be the one that explains your dilemma?
Image

Klaus
Posts: 13829
Joined: Sat Apr 08, 2006 8:41 am
Location: Germany
Contact:

Re: Parsing arrays

Post by Klaus » Wed Jan 01, 2020 8:03 pm

WebiWan wrote:
Wed Jan 01, 2020 7:48 pm
...I got the routine cmdCursor2Array from these pages but here it is too:

Code: Select all

command cmdCursor2Array pCursor, @pOutArrayA
   ## Get the names of all the columns in the database cursor
   put revDatabaseColumnNames(pCursor) into theFields
   if theFields begins with "revdberr," then
      put item 2 to -1 of theFields into theError
   end if
   
Where is the rest of the script?

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 9669
Joined: Wed May 06, 2009 2:28 pm
Location: New York, NY

Re: Parsing arrays

Post by dunbarx » Wed Jan 01, 2020 10:13 pm

Hi.

If you create and populate an array by hand, like in the test I offered, I will assume you get a good result.

But I do not see anything in your post that tells me how the variable "myArray" is populated, that is, populated in a way that will indeed create an array variable. It simply is "used" to extract data into ordinary variables.

How is "myArray" built?

Richard asked this at the beginning. There must be a point in your handler that can be stopped with a breakpoint, and at that line determine if you actually have an array in the variable "myArray".

Do this first, then post back. Or post the script, and we will find out.

Craig

FourthWorld
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 9842
Joined: Sat Apr 08, 2006 7:05 am
Location: Los Angeles
Contact:

Re: Parsing arrays

Post by FourthWorld » Wed Jan 01, 2020 11:20 pm

WebiWan wrote:
Wed Jan 01, 2020 7:34 pm
The only thing I can think of is the subkeys are not numbered, they are named.
Bingo. A name may include alphabetic characters or numerals or punctuation or any combination of those. But it only has one name.

If you name it "Steve" and you ask for "Bob", Steve will not answer.

Same if you name it with the string "CompName" and ask for it with the string "3".

Had we seen the code populating the array this would have been immediately evident.
But I assumed indexing them with a number would work. Like any good array should.
There are two main types of arrays in computer science: indexed and associative.

Neither is "good" or "bad". They each have a useful role.

Indexed arrays have no name, addressed by ordinal position in a linked list, expressed as an integer.

Associative arrays are name-value pairs.

While LiveCode Builder offers both, LiveCode Script currently offers only associative arrays.

Retrieve its value with the same name you set the value with and you'll always get what you want.

Note: I've been working with LiveCode since before version 1.0, and as community manager maintain a close relationship with both the core dev team and much of the community. Through the experience gained from these communications over the years I've found that most recommendations to revert to an earlier version to attempt to solve a problem which appears vastly pervasive and omni-platform are incorrect. In such cases it helps to following diagnostic guidance, which usually yields a simple syntax error in the script, as was the case here.

This is not to suggest LiveCode is the only software of its magnitude without bugs. Far from it. But the combination of automated testing and community testing usually keeps bugs in any released version to those far more specialized in nature.
Richard Gaskin
LiveCode development, training, and consulting services: Fourth World Systems
LiveCode Group on Facebook
LiveCode Group on LinkedIn

WebiWan
Posts: 24
Joined: Fri Apr 20, 2018 2:21 am

Re: Parsing arrays

Post by WebiWan » Thu Jan 02, 2020 7:32 pm

Klaus wrote:
Wed Jan 01, 2020 8:03 pm
WebiWan wrote:
Wed Jan 01, 2020 7:48 pm
...I got the routine cmdCursor2Array from these pages but here it is too:

Code: Select all

command cmdCursor2Array pCursor, @pOutArrayA
   ## Get the names of all the columns in the database cursor
   put revDatabaseColumnNames(pCursor) into theFields
   if theFields begins with "revdberr," then
      put item 2 to -1 of theFields into theError
   end if
   
Where is the rest of the script?
Sorry, not sure what happened...

Code: Select all

command cmdCursor2Array pCursor, @pOutArrayA
   ## Get the names of all the columns in the database cursor
   put revDatabaseColumnNames(pCursor) into theFields
   if theFields begins with "revdberr," then
      put item 2 to -1 of theFields into theError
   end if
   	
   if theError is empty then
      put 0 into i
      ## Loop through all rows in cursor
      repeat until revQueryIsAtEnd(pCursor)
         add 1 to i
         			
         ## Move all fields in row into next dimension of the array
         repeat for each item theField in theFields
            put revDatabaseColumnNamed(pCursor, theField) into pOutArrayA[i][ theField ]
         end repeat
         			
         revMoveToNextRecord pCursor
      end repeat
   end if
end cmdCursor2Array

WebiWan
Posts: 24
Joined: Fri Apr 20, 2018 2:21 am

Re: Parsing arrays

Post by WebiWan » Thu Jan 02, 2020 7:36 pm

bogs wrote:
Wed Jan 01, 2020 7:56 pm
I know you had a lot of responses and sent a lot of replies, but did you see this response from Axwald, which might be the one that explains your dilemma?
I did, thanks. But it's going to take me a little while to comprehend it all. :oops:

Klaus
Posts: 13829
Joined: Sat Apr 08, 2006 8:41 am
Location: Germany
Contact:

Re: Parsing arrays

Post by Klaus » Thu Jan 02, 2020 7:46 pm

Klaus wrote:
Wed Jan 01, 2020 8:03 pm
Where is the rest of the script?
Sorry, not sure what happened...

Code: Select all

command cmdCursor2Array pCursor, @pOutArrayA
   ## Get the names of all the columns in the database cursor
   put revDatabaseColumnNames(pCursor) into theFields
   if theFields begins with "revdberr," then
      put item 2 to -1 of theFields into theError
   end if
   	
   if theError is empty then
      put 0 into i
      ## Loop through all rows in cursor
      repeat until revQueryIsAtEnd(pCursor)
         add 1 to i
         			
         ## Move all fields in row into next dimension of the array
         repeat for each item theField in theFields
            put revDatabaseColumnNamed(pCursor, theField) into pOutArrayA[i][ theField ]
         end repeat
         			
         revMoveToNextRecord pCursor
      end repeat
   end if
end cmdCursor2Array
AHA, OK, someone already mentioned this, the keys of your ARRAY are NAMED and not numbers!
So you need of course to do something like this:

Code: Select all

...
repeat for each key tKey in myArray
   put myArray[tKey]["name of db column 1"] into tvMake
   put myArray[tKey]["name of db column 2"] into tvModel 
   etc...
end repeat
...
Best

Klaus

WebiWan
Posts: 24
Joined: Fri Apr 20, 2018 2:21 am

Re: Parsing arrays

Post by WebiWan » Thu Jan 02, 2020 7:53 pm

FourthWorld wrote:
Wed Jan 01, 2020 11:20 pm
WebiWan wrote:
Wed Jan 01, 2020 7:34 pm
The only thing I can think of is the subkeys are not numbered, they are named.
Bingo. A name may include alphabetic characters or numerals or punctuation or any combination of those. But it only has one name.
I just looked back through the other programs I've produced with Livecode and I can see numerous times I've done it correctly. I don't know why I had trouble this time around. :oops:

I'll make the change in the next day or so and get back to everyone about the results... just to tie up loose ends.

Thanks everyone!

P.S. I was too excited to let it lay about. I tried it and it's working fine. Thanks again to everyone. I'm sorry my denseness put everyone through this. Happy New Year!
Last edited by WebiWan on Thu Jan 02, 2020 8:12 pm, edited 1 time in total.

Klaus
Posts: 13829
Joined: Sat Apr 08, 2006 8:41 am
Location: Germany
Contact:

Re: Parsing arrays

Post by Klaus » Thu Jan 02, 2020 8:00 pm

WebiWan wrote:
Thu Jan 02, 2020 7:53 pm
I don't know why I had trouble this time around. :oops:
Don't worry, to quote the great band YES:
It could happen to you
It could happen to me
It could happen to everyone
Eventually
I even once opened a bug report due to my personal "temporary blindness"! :D

Simon Knight
Posts: 854
Joined: Wed Nov 04, 2009 11:41 am
Location: Gunthorpe, North Lincs, UK

Re: Parsing arrays

Post by Simon Knight » Sat Mar 14, 2020 11:25 am

Hi and bump!

Just wanted to say that this thread helped clarify a few points about arrays - so thanks all.
best wishes
Skids

Klaus
Posts: 13829
Joined: Sat Apr 08, 2006 8:41 am
Location: Germany
Contact:

Re: Parsing arrays

Post by Klaus » Sat Mar 14, 2020 11:47 am

Makes five quid then! :-D

Simon Knight
Posts: 854
Joined: Wed Nov 04, 2009 11:41 am
Location: Gunthorpe, North Lincs, UK

Re: Parsing arrays

Post by Simon Knight » Sat Mar 14, 2020 12:14 pm

I even once opened a bug report due to my personal "temporary blindness"! :D

Just the once - you are a marvel ! ;-)


As to arrays I think that some of the LC examples are unnecessarily complicated:

Code: Select all

# declare the arrays we are going to populate
local tLineArray1, tLineArray2, tArrayInArray
# populate the one dimensional arrays tLineArray1 and tLineArray2
put "O" into tLineArray1[1]
put "X" into tLineArray1[2]
put " " into tLineArray2[1]
put "O" into tLineArray2[2]
# store the one dimensional arrays in tArrayInArray to create the two dimensional array
put tLineArray1 into tArrayInArray[1]
put tLineArray2 into tArrayInArray[2]
While I accept that the code is demonstrating storing several arrays inside a parent the code is the first on the page http://lessons.livecode.com/m/4071/l/12 ... y-variable

The array names don't help, I much prefer this version:

Code: Select all

# declare the arrays we are going to populate
local tLine1_A, tLine2_A, tAllLines_A

# populate the one dimensional arrays tLine1_A and tLine2_A
put "O" into tLine1_A[1]
put "X" into tLine1_A[2]

put " " into tLine2_A[1]
put "O" into tLine2_A[2]

# store the one dimensional arrays in tAllLines_A to create the two dimensional array
put tLine1_A into tAllLines_A[1]
put tLine2_A into tAllLines_A[2]
but it could just be me!
best wishes
Skids

Post Reply

Return to “Getting Started with LiveCode - Experienced Developers”