Page 1 of 1

Associative Arrays & STDIN support

Posted: Thu Nov 10, 2005 5:50 pm
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

Posted: Thu Nov 10, 2005 6:04 pm
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"

Posted: Thu Nov 10, 2005 6:27 pm
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:

Posted: Fri Nov 11, 2005 12:35 pm
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 :-(

Posted: Fri Nov 11, 2005 5:54 pm
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....

Posted: Fri Nov 11, 2005 8:38 pm
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 :).

Posted: Fri Nov 11, 2005 9:56 pm
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.

Posted: Mon Nov 14, 2005 1:16 pm
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.

Posted: Mon Jul 02, 2007 3:44 am
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 :)