ScriptBasic
May 22, 2012, 01:45:09 AM *
Welcome, Guest. Please login or register.

Login with username, password and session length
News: Registration Disabled Send an e-mail to support @ scriptbasic.org to request a membership to this forum. (include desired forum name)
 
   Home   Wiki Help Search Login Register  
Pages: [1]
  Print  
Author Topic: Arrays in ScriptBasic  (Read 929 times)
support
Administrator
*****
Posts: 409


« 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:
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:
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:
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:
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"

Logged
Pages: [1]
  Print  
 
Jump to:  

Powered by MySQL Powered by PHP Powered by SMF 1.1.16 | SMF © 2011, Simple Machines Valid XHTML 1.0! Valid CSS!