Author Topic: Arrays in ScriptBasic  (Read 15205 times)

Support

  • Administrator
  • *****
  • Posts: 22
    • View Profile
Arrays in ScriptBasic
« on: October 08, 2009, 10:50:15 AM »
Here is an example of using a mix of associative and standard arrays. I see this as a way to build/emulate TYPE/UNION structures in SB.

Code: [Select]
a[1]="John Doe"
a[2]="123 St."
a[3]="Anytown,USA"
a[4,1]="800-THE-SHOP"
a[4,2]="900-OUR-CELL"

cust{"addr_blk"} = a

PRINT cust{"addr_blk"}[1],"\n"
PRINT cust{"addr_blk"}[2],"\n"
PRINT cust{"addr_blk"}[3],"\n"
PRINT cust{"addr_blk"}[4,1],"\n"
PRINT cust{"addr_blk"}[4,2],"\n"

C:\scriptbasic\test>aaa
John Doe
123 St.
Anytown,USA
800-THE-SHOP
900-OUR-CELL
C:\scriptbasic\test>


This example uses three associative arrays to provide a customer address block structure.

Code: [Select]
IMPORT t.bas

p{"shop"} = "800-THE-SHOP"
p{"cell"} = "900-OUR-CELL"

a{"name"} = "John Doe"
a{"addr1"} = "123 St."
a{"addr2"} = "Anytown,USA"
a{"phone"} = p

cust{"addr_blk"} = a

PRINT cust{"addr_blk"}{"name"},"\n"
PRINT cust{"addr_blk"}{"addr1"},"\n"
PRINT cust{"addr_blk"}{"addr2"},"\n"
PRINT cust{"addr_blk"}{"phone"}{"shop"},"\n"
PRINT cust{"addr_blk"}{"phone"}{"cell"},"\n"

xmlstr = t::ArrayToXML(cust)

PRINT xmlstr,"\n"

Code: [Select]
C:\scriptbasic\test>aaaa
John Doe
123 St.
Anytown,USA
800-THE-SHOP
900-OUR-CELL
<?xml version="1.0" encoding="UTF-8"?><V><A l="0" h="1"><S>addr_blk</S><A l="0" h="7"><S>name</S><S>John Doe</S><S>addr1</S><S>123 St.</S><S>addr2</S><S>Anytown,USA</S><S>phone</S><A l="0" h="3"><S>shop</S><S>800-THE-SHOP</S><S>cell</S><S>900-OUR-CELL</S></A></A></A></V>
C:\scriptbasic\test>



Another variation for fun.

Code: [Select]
IMPORT t.bas

n[1] = "name"
n[2] = "addr1"
n[3] = "addr2"
n[4] = "phone"
n[4,1] = "shop"
n[4,2] = "cell"

p{n[4,1]} = "800-THE-SHOP"
p{n[4,2]} = "900-OUR-CELL"

a{n[1]} = "John Doe"
a{n[2]} = "123 St."
a{n[3]} = "Anytown,USA"
a{n[4]} = p

cust{"addr_blk"} = a

PRINT cust{"addr_blk"}{"name"},"\n"
PRINT cust{"addr_blk"}{"addr1"},"\n"
PRINT cust{"addr_blk"}{"addr2"},"\n"
PRINT cust{"addr_blk"}{"phone"}{"shop"},"\n"
PRINT cust{"addr_blk"}{"phone"}{"cell"},"\n"

xmlstr = t::ArrayToXML(cust)

PRINT xmlstr,"\n"