[Implemented] Associative Arrays

Got an idea for enhancing PureBasic? New command(s) you'd like to see?
Karbon
PureBasic Expert
PureBasic Expert
Posts: 2010
Joined: Mon Jun 02, 2003 1:42 am
Location: Ashland, KY
Contact:

[Implemented] Associative Arrays

Post by Karbon »

Bam. I know it's not a BASIC construct but I think it's the single most useful thing about PHP and other languages that have them as native variable types.

Anyway, just wanted to add that to the stack of feature requests :-)
-Mitchell
Check out kBilling for all your billing software needs!
http://www.k-billing.com
Code Signing / Authenticode Certificates (Get rid of those Unknown Publisher warnings!)
http://codesigning.ksoftware.net
User avatar
fsw
Addict
Addict
Posts: 1603
Joined: Tue Apr 29, 2003 9:18 pm
Location: North by Northwest

Post by fsw »

What is this :?:

A short explanation would be nice.

Maybe this is something I needed for ages and didn't know about it 8O

I am to provide the public with beneficial shocks.
Alfred Hitshock
Master C
New User
New User
Posts: 6
Joined: Sat Aug 16, 2003 7:35 pm

Post by Master C »

Data in associative arrays aren't called by an index, but by an keyword:

Normal array:
person(0)="John"
person(1)="Banner"

Associative array:
person("prename")="John"
person("lastname")="Banner"
User avatar
Inner
PureBasic Expert
PureBasic Expert
Posts: 714
Joined: Fri Apr 25, 2003 4:47 pm
Location: New Zealand

Post by Inner »

already can;

dim array.s(2)

#NAME=0
#SURNAME=1

array(0)="Inner"
array(1)="Space"

debug(array(#NAME))
debug(array(#SURNAME))

Or am I missing something ?
Fred
Administrator
Administrator
Posts: 18162
Joined: Fri May 17, 2002 4:39 pm
Location: France
Contact:

Post by Fred »

It's so called 'HashMap' array, which has a key (basically a string) and a value. It's to have faster/easier data handling when your key can't be a sequential number

Code: Select all


a$ = "Book"
b$ = "Book2"

a(a$) = "Author1"
a(b$) = "Author2"

It heavily used in JAVA for example. I may introduce this kind of data storage later.
Karbon
PureBasic Expert
PureBasic Expert
Posts: 2010
Joined: Mon Jun 02, 2003 1:42 am
Location: Ashland, KY
Contact:

Post by Karbon »

Fred, excellent to hear!
-Mitchell
Check out kBilling for all your billing software needs!
http://www.k-billing.com
Code Signing / Authenticode Certificates (Get rid of those Unknown Publisher warnings!)
http://codesigning.ksoftware.net
Karbon
PureBasic Expert
PureBasic Expert
Posts: 2010
Joined: Mon Jun 02, 2003 1:42 am
Location: Ashland, KY
Contact:

Post by Karbon »

Thread resurrection :-)

4 years past (wow!), just checking...
-Mitchell
Check out kBilling for all your billing software needs!
http://www.k-billing.com
Code Signing / Authenticode Certificates (Get rid of those Unknown Publisher warnings!)
http://codesigning.ksoftware.net
thefool
Always Here
Always Here
Posts: 5875
Joined: Sat Aug 30, 2003 5:58 pm
Location: Denmark

Post by thefool »

Actually i could use this right now :p
Well, i will just have to do my own.
DarkDragon
Addict
Addict
Posts: 2344
Joined: Mon Jun 02, 2003 9:16 am
Location: Germany
Contact:

Post by DarkDragon »

Haven't I asked for this a few years ago? :lol:
bye,
Daniel
User avatar
fsw
Addict
Addict
Posts: 1603
Joined: Tue Apr 29, 2003 9:18 pm
Location: North by Northwest

Post by fsw »

Karbon wrote:Thread resurrection :-)

4 years past (wow!), just checking...
You are walking on dangerous ground.

Last time somebody resurrected a thread, Freak got all :evil:

:shock:
Karbon
PureBasic Expert
PureBasic Expert
Posts: 2010
Joined: Mon Jun 02, 2003 1:42 am
Location: Ashland, KY
Contact:

Post by Karbon »

My middle name is danger. Mitchell Danger Vincent.. That's me :-)
-Mitchell
Check out kBilling for all your billing software needs!
http://www.k-billing.com
Code Signing / Authenticode Certificates (Get rid of those Unknown Publisher warnings!)
http://codesigning.ksoftware.net
Trond
Always Here
Always Here
Posts: 7446
Joined: Mon Sep 22, 2003 6:45 pm
Location: Norway

Post by Trond »

I don't really see the use for this. Of course it's handy, but it will be so much slower than normal arrays.
Karbon
PureBasic Expert
PureBasic Expert
Posts: 2010
Joined: Mon Jun 02, 2003 1:42 am
Location: Ashland, KY
Contact:

Post by Karbon »

Speed has nothing to do with it as there is no way you could compare the functionality of a hash table (AKA associative array) with that of a "plain" array. You simple can't do this with an array, fast or slow :

Name.s = TheSettings("name")

(IE, string, or *any* non-integer variable type indexes)

You can get almost the same functionality with the linked lists (especially with the sorting) but there are still plenty of uses for "real" hash tables.

I'm not saying PB is useless or anything but it's a very valuable language construct just the same :-)
-Mitchell
Check out kBilling for all your billing software needs!
http://www.k-billing.com
Code Signing / Authenticode Certificates (Get rid of those Unknown Publisher warnings!)
http://codesigning.ksoftware.net
User avatar
Flype
Addict
Addict
Posts: 1542
Joined: Tue Jul 22, 2003 5:02 pm
Location: In a long distant galaxy

Post by Flype »

I agree about the speed,
but speed is not a problem in all case, everytime, everywhere.

9 times on 10 i don't need run-time speed (ok the more it is, the better), but coding-time speed.

Such 'associative array' or also called 'dictionary' (search those keywords on the forum, there are some includes) are very useful, and powerful. Such data-structures are used in many languages (PHP, Python, Java, ...)

Particularly when working with SQL, parsing sort of INI files, parsing arguments of command-lines, but not only...
No programming language is perfect. There is not even a single best language.
There are only languages well suited or perhaps poorly suited for particular purposes. Herbert Mayer
Karbon
PureBasic Expert
PureBasic Expert
Posts: 2010
Joined: Mon Jun 02, 2003 1:42 am
Location: Ashland, KY
Contact:

Post by Karbon »

Indeed, it's a *really* nice way of handling arbitrary length non-numeric "collections" of data.

PHP's array handling functions are about the best of any language I've seen. I realize that is due in large part to PHP's loose typing, something PB doesn't (and shouldn't, IMHO) support.

Anyway, just dropping this note back in the suggestion box :-)
-Mitchell
Check out kBilling for all your billing software needs!
http://www.k-billing.com
Code Signing / Authenticode Certificates (Get rid of those Unknown Publisher warnings!)
http://codesigning.ksoftware.net
Post Reply