|
Post by konaexpress on Mar 8, 2014 18:09:03 GMT 1
Can anyone tell me how to describe this and how it is used?
'### BaCon: Associative Array. Terry Becker SunBurnt Oct. 7, 2012
' Make associative array. DECLARE Cars$ ASSOC STRING ' String array as index of assoc. array. 'DECLARE I$ I$ = "item1 item2 item3" SPLIT I$ BY " " TO item$ SIZE iSize ' String array of data for assoc. array. 'DECLARE C$ C$ = "Camero Mustang Challenger" SPLIT C$ BY " " TO data$ SIZE dSize
PRINT NL$, "### Hot cars are:", NL$
'############## Print the array items: item1, item2, item3 FOR i = 0 TO 2 Cars$(item$[i]) = data$[i] PRINT Cars$(item$[i]) NEXT
PRINT
Thanks, John
|
|
|
Post by Pjot on Mar 9, 2014 9:11:34 GMT 1
John, Next to the index arrays BaCon also can use associative arrays. This code declares such an associative array, then assigns two strings with values which are SPLIT'ed and whose resulting arrays are used as index and value to assign to the associative array. This code is somewhat complicated; if it would just be about initialization then it could have been written more easily (see below). The concept of an associative array is explained in Wikipedia. HTH Peter ' Declare array DECLARE Cars$ ASSOC STRING
' Initialize Cars$("item1") = "Camero" Cars$("item2") = "Mustang" Cars$("item3") = "Challenger"
PRINT NL$, "### Hot cars are:", NL$
' Lookup the index names (item1, item2, item3) LOOKUP Cars$ TO item$ SIZE size
' Print values FOR i = 0 TO size-1 PRINT Cars$(item$[i]) NEXT
|
|
|
Post by alexfish on Mar 10, 2014 2:44:06 GMT 1
Hi John
Can also build up complex assoc arrays ,
Not sure if You got any children named as in this demo, but can substitute the bits
Also in the Bacon Docs it mentions a Language called PROLOG , Prolog is sometimes used for problem solving , IE can find missing bits of a puzzle
as in like Logic Puzzles.
anyway , here is a taster of a three up , sort of a family Tree
Have Fun Alex
Here We Go
'@ For Tree Types add extra at end to prevent recursive/lookup DECLARE TREE_MALE$ ,PARENT$, CHILD$,ANY$ ASSOC STRING
RELATE TREE_MALE$ TO PARENT$ RELATE TREE_MALE$ TO CHILD$ TREE_MALE$("Male Parent")="John" TREE_MALE$("John")="Mary" '@ Find The bits parent1$= PARENT$("Male Parent") kin$= CHILD$(parent1$)
PRINT " Male Parent ",parent1$," has a child called ",kin$
|
|
|
Post by bigbass on Sept 2, 2019 18:51:54 GMT 1
just adding to the mix another demo for associative arrays that I used to see what is happening and adding OBTAIN$ NRKEYS
DECLARE name,bandmember ASSOC int RELATE name TO bandmember
name("John") = 1 name("Paul") = 2 name("Ringo") = 3 name("George") = 4
PRINT PRINT "beatles are:" LOOKUP name TO bandmember$ SIZE amount FOR x = 0 TO amount - 1 PRINT bandmember$[x] NEXT PRINT
PRINT "band members are:" LOOKUP bandmember TO bandmember$ SIZE amount FOR x = 0 TO amount - 1 PRINT bandmember$[x] NEXT PRINT
'--- OBTAIN$ example they were RELATED PRINT OBTAIN$(name) PRINT OBTAIN$(bandmember) PRINT
'--- NRKEYS get total number of arrays in the assoc array PRINT "Total number of name: ", NRKEYS(name) PRINT "Total number of bandmember: ", NRKEYS(bandmember) PRINT
'--- get the value that Ringo is declared as PRINT "Ringo is number ", name("Ringo")
terminal output
beatles are: John Paul Ringo George
band members are: John Paul Ringo George
John Paul Ringo George John Paul Ringo George
Total number of name: 4 Total number of bandmember: 4
Ringo is number 3
|
|
|
Post by bigbass on Sept 16, 2019 18:32:18 GMT 1
another example using strings and getting the stored values
DECLARE name ASSOC STRING
name("John") = "Lennon" name("Paul") = "McCartney" name("Ringo") = "Starr" name("George") = "Harrison"
PRINT '--- we print the fist name it is indexed '--- using the LOOKUP command PRINT "beatles are:" LOOKUP name TO the_name$ SIZE amount FOR x = 0 TO amount - 1 PRINT the_name$[x] ,"'s Last name is ", name(the_name$[x]) NEXT PRINT
'--- OBTAIN$ example PRINT OBTAIN$(name)
PRINT
'--- NRKEYS get total number of arrays in the assoc array PRINT "Total number of name: ", NRKEYS(name)
PRINT
'--- get the value that Ringo is declared as '--- in this case it is the last name PRINT "Ringo Last name is ", name("Ringo") PRINT
'--- another way using SPLIT and OBTAIN instead of LOOKUP '--- place in a string delimited by a space '--- we get the last name from the FIRSTNAME$ indexed FIRSTNAME$ = OBTAIN$(name) SPLIT FIRSTNAME$ BY " " TO array$ SIZE dimension FOR i = 0 TO dimension -1 '--- how to get both key and value pairs PRINT array$[i] ,"'s Last name is ", name(array$[i]) NEXT PRINT
PRINT "sorted first names:" PRINT SORT$( FIRSTNAME$," ")
TERMINAL OUTPUT
beatles are: John's Last name is Lennon Paul's Last name is McCartney Ringo's Last name is Starr George's Last name is Harrison
John Paul Ringo George
Total number of name: 4
Ringo Last name is Starr
John's Last name is Lennon Paul's Last name is McCartney Ringo's Last name is Starr George's Last name is Harrison
sorted first names: George John Paul Ringo
|
|