Home   Archive   Permalink



Associative arrays - remove

How can I remove key/val pair from block (associative array)?
    
; Associative arrays in rebol are just blocks. To create one, create block:
aarray: copy []
    
; Or initialized with values:
aarray: [
    "foo" "bar"
    "url" http://rebol.com
    222 135
]
    
; print mold aarray
; ---
; [
;     "foo" "bar"
;     "url" http://rebol.com
;     222 135
; ]
    
    
    
; Adding new values
append aarray ["key" "val"]
    
; print mold aarray
; ---
; [
;     "foo" "bar"
;     "url" http://rebol.com
;     222 135 "key" "val"
; ]
    
    
    
; Updating values
change select aarray "key" "new val"
    
; print mold aarray
; -
; [
;     "foo" "bar"
;     "url" http://rebol.com
;     222 135 "key" "new val"
; ]

posted by:   RF       11-Jun-2013/10:39:31-7:00



I've tried;
    
remove aarray "key"
    
But that removed "foo", not "key" or both "key" "new val".
    
remove remove find/skip aarray "key" 2
    
Seems to work, but it is kinda ugly. Is there better way how to work with associative arrays in rebol?

posted by:   RF       11-Jun-2013/11:52:59-7:00



If you're using associative arrays in Rebol v3, then you get the map! type. There removal is simply setting the value to none!.
    
If you are bound to using blocks, there is 'remove-each:
    
     remove-each [key value] aarray [key = "key"]

posted by:   Chris       11-Jun-2013/23:53:13-7:00



Chris: I am not limited to blocks, I just didn't found anything else.
    
Is there any paper explaining how to do basic operations (create/pick/update/remove) with associative arrays in r2 and r3?

posted by:   RF       12-Jun-2013/1:45:50-7:00



There aren't really associative arrays as blocks in REBOL, so REMOVE is therefore simple, so you need something like:
    
remove remove find/skip aarray "key" 2
    
In R3, you have the map! type, which is very fast or the ability to extend and remove words from object!s.

posted by:   Henrik       20-Jun-2013/16:54:24-7:00



You can also use /part instead of remove twice:
    
     remove/part find/skip aarray "key" 2


posted by:   Endo       6-Aug-2013/4:59:10-7:00



submit response

posted by:   r e t i m i l e d - y h c r a r e i h       4-Nov-2013/21:48:42-8:00