You may also interested to know J has jfile/keyfile/kfile
packages either in its base library or as addons. They use
3!:1/3!:2 so perhaps more efficient.

Чт, 26 сен 2013, Pascal Jasmin писал(а):
> I noticed that some of you have been looking for or talked about a YAML 
> implementation.  J actually has a great portable format built in with 5!:5, 
> though maybe there needs a human readable output that could be as simple as 
> indenting after <.
> 
> Here are some verbs for working with key,value data in trees.  They're pretty 
> forgiving about boxing parameters, and intended mostly for display and small 
> datasets.
> 
> NB. key value boxed data.  All verbs use y as the kv store, and x as the 
> query/new val
> kvgi =: #@:{.@:]  _:`]@.> (boxopen@[ i.~  (0&{@:]))"0 _  NB. get index
> kvadd =: 4 : 0 NB. assumes at least one row.  Appends if key exists
> 
> i=. ({.x) kvgi y 
> if. _=i do.  y ,."_ 1 x else. ,&(>}. x) L:0 amend i amend 1 y end.
> )
> kvset =: 4 : 0 NB. assumes at least one row.  Replaces if key exists
> i=. ({.x) kvgi y 
> if. _=i do.  y ,."_ 1 x else. (> }.x)"1 L:0 amend i amend 1 y end.
> )
> kvgv =: ((1&{@:]) {~  boxopen@[ i.~  (0&{@:]))"0 _ :: empty  NB. get value
> kvgk =: ((0&{@:]) {~  boxopen@[ i.~  (1&{@:]))"0 _ :: empty  NB. get first key
> kvlabel =: boxopen@[ , boxopen@:]                            NB. turns value 
> into k,v
> 
> 
> 
>    ] h=. (1;6) kvset (5; 4) kvadd (5; 3 ) kvadd (5;2) kvadd (3;1) kvadd 
> ('a';3) kvadd (3;4) kvadd 1 kvlabel 2
> ┌─┬───┬─┬─────┐
> │1│3  │a│5    │
> ├─┼───┼─┼─────┤
> │6│4 1│3│2 3 4│
> └─┴───┴─┴─────┘
>    5 kvgv h
> ┌─────┐
> │2 3 4│
> └─────┘
>  
>   (('list2';3) kvset L:1 (<'list')&kvlabel L:0) amend 3 amend 1 h
> ┌─┬───┬─┬─────────────┐
> │1│3  │a│5            │
> ├─┼───┼─┼─────────────┤
> │6│4 1│3│┌─────┬─────┐│
> │ │   │ ││list │list2││
> │ │   │ │├─────┼─────┤│
> │ │   │ ││2 3 4│3    ││
> │ │   │ │└─────┴─────┘│
> └─┴───┴─┴─────────────┘
> 
>    (<5) kvgv (('list2';3) kvset L:1 (<'list')&kvlabel L:0) amend 3 amend 1 h
> ┌─────────────┐
> │┌─────┬─────┐│
> ││list │list2││
> │├─────┼─────┤│
> ││2 3 4│3    ││
> │└─────┴─────┘│
> └─────────────┘
> 
> 
> ] h2=. ('sidebar';'<') kvadd 'title' kvlabel < (('list2';3) kvset L:1 
> (<'list')&kvlabel L:0) amend 3 amend 1 h
> ┌───────────────────────┬───────┐
> │title                  │sidebar│
> ├───────────────────────┼───────┤
> │┌─┬───┬─┬─────────────┐│<      │
> ││1│3  │a│5            ││       │
> │├─┼───┼─┼─────────────┤│       │
> ││6│4 1│3│┌─────┬─────┐││       │
> ││ │   │ ││list │list2│││       │
> ││ │   │ │├─────┼─────┤││       │
> ││ │   │ ││2 3 4│3    │││       │
> ││ │   │ │└─────┴─────┘││       │
> │└─┴───┴─┴─────────────┘│       │
> └───────────────────────┴───────┘
> 
>    5 kvgv > (<'title') kvgv h2
> ┌─────────────┐
> │┌─────┬─────┐│
> ││list │list2││
> │├─────┼─────┤│
> ││2 3 4│3    ││
> │└─────┴─────┘│
> └─────────────┘
> 
>    5!:5 <'h2'
> 2 2$(<'title'),(<'sidebar'),(<2 4$(<1),(<3),(<'a'),(<5),(<,6),(<4 1),(<3),<2 
> 2$'list';'list2';2 3 4;3),<'<'
> ----------------------------------------------------------------------
> For information about J forums see http://www.jsoftware.com/forums.htm

-- 
regards,
====================================================
GPG key 1024D/4434BAB3 2008-08-24
gpg --keyserver subkeys.pgp.net --recv-keys 4434BAB3
----------------------------------------------------------------------
For information about J forums see http://www.jsoftware.com/forums.htm

Reply via email to