Associative Arrays & STDIN support

Got an idea for enhancing PureBasic? New command(s) you'd like to see?
naw
Enthusiast
Enthusiast
Posts: 573
Joined: Fri Apr 25, 2003 4:57 pm

Associative Arrays & STDIN support

Post by naw »

Associative arrays are great - could we have them in PB?

For those who havent come across them:

Dim me.s(3)
me[name]="Nigel"
me[age]="36"
me[sex]="male"

STDIN is soooo powerful and it's a must-have for any Linux language, please can we have some simple means to redirect STDIN into PB, perhaps an adaption of RunProgram() that just redirects to a special global variable called STDIN$.

Ta - N
Ta - N
Trond
Always Here
Always Here
Posts: 7446
Joined: Mon Sep 22, 2003 6:45 pm
Location: Norway

Post by Trond »

1. I'd love to get stdin and stdout support.
2. About the associative arrays, do you mean something like this?

Code: Select all

Enumeration
  #Name
  #Age
  #Sex
EndEnumeration

Dim You.s(2)
You(#Name) = "Nigel"
You(#Age)  = "36"
You(#Sex)  = "Twice a week"
Polo
Addict
Addict
Posts: 2422
Joined: Tue May 06, 2003 5:07 pm
Location: UK

Post by Polo »

I think he meant arrays that act like C's one, though I'm not very sure...
You(#Name) = "Nigel"
You(#Age) = "36"
You(#Sex) = "Twice a week"
Tss :lol:
naw
Enthusiast
Enthusiast
Posts: 573
Joined: Fri Apr 25, 2003 4:57 pm

Post by naw »

Thats the way I have to do it (using constants and enumeration , but I really do mean:
Dim.me.s(3)
me("name")="Nigel"
me("age")="36"
me("sex")="not often enough :-)"

(dunno if this is like C or not - I know Associative Arrays from Korn Shell and Perl)

Though if it were a choice between Associatyive Arrays and STDIN support, then STDIN wins hands down. I started writing a Sys Admin tool for Linux that mimicked AIX & HPUX's Sysadmin tools. I abandoned it because there is no STDIN support :-(
Ta - N
dell_jockey
Enthusiast
Enthusiast
Posts: 767
Joined: Sat Jan 24, 2004 6:56 pm

Post by dell_jockey »

correct me if I'm wrong, but under the Unix/Linux I/O paradigma that 'everything is a file', can't you just open STDIN, STDOUT, STDERR like you would any other file? What's missing in PB that this is not possible, perhaps just a symbolic mapping to the correct IOCTL-block?

Just wondering....
cheers,
dell_jockey
________
http://blog.forex-trading-ideas.com
Fred
Administrator
Administrator
Posts: 18162
Joined: Fri May 17, 2002 4:39 pm
Location: France
Contact:

Post by Fred »

The associative arrays are often known as 'HashMap', which work as a 'key'/'value' storage, where the key can be anything unique (like an address, a string etc..). It's something i would like to implement natively in PB since a long time, but it won't probably happen with v4.0 as some other more important things are planned. Anyway, it's a good wish and it's on the todo list :).
Trond
Always Here
Always Here
Posts: 7446
Joined: Mon Sep 22, 2003 6:45 pm
Location: Norway

Post by Trond »

dell_jockey wrote:correct me if I'm wrong, but under the Unix/Linux I/O paradigma that 'everything is a file', can't you just open STDIN, STDOUT, STDERR like you would any other file? What's missing in PB that this is not possible, perhaps just a symbolic mapping to the correct IOCTL-block?

Just wondering....
I think you can do something like that on Windows too.
naw
Enthusiast
Enthusiast
Posts: 573
Joined: Fri Apr 25, 2003 4:57 pm

Post by naw »

re: STDIN. Well from a Linux/Unix ksh or bash, then yes you can just redirect it the same as you would a file:

eg: cat myFile | myScript.ksh

or: ls -l | awk '/^d/ { print $NF }'

or: print "1 2 3" | awk '/^d/ { print $1, $NF }' 2>/dev/null

or: print `ls -l` | awk '/^d/ { print $1, $NF }' 2>/dev/null

or: ls -l /sdsdsd 2>/dev/null | awk '/^d/ { print $1, $NF }'

this sort of flexibility with commands, files and redirection of input and output makes Unix scripting v-e-r-y powerful and extendible. STDOUT is easy to do from within PB, but not STDIN - It would be invaluable.
Ta - N
User avatar
Rescator
Addict
Addict
Posts: 1769
Joined: Sat Feb 19, 2005 5:05 pm
Location: Norway

Post by Rescator »

For those curious (and as a sneaky bump :P http://php.net/manual/en/ref.array.php

PHP's arrays are also associative and once you start getting used to them you'll love them.

It would certainly make cross porting PB and PHP apps very easy for folks like me since I use associative arrays a lot in PHP.

Fred, I was thinking though, can't you just copy the existing PB list code and tweak it to have a hashmap? That should provide the core functionality of associate arrays and most of the existing list code could be copied and reused as well with minimal editing I hope?
I.e copy LinkedList lib into a LinkedListAssociative if you know what I mean :)
Post Reply