Munge

Munge is a very useful function that enables you to get blocks of tabular (row and column) data from a variety of sources, manipulate the data in various ways, then store the result as a REBOL block or even save back as a CSV or Excel file!

Here are some simple examples of loading data:

>> load-dsv %test.csv
== [["a" "1"] ["b" "2"] ["c" "3"]]
>> load-excel %test.xlsx 1
== [["a" "1"] ["b" "2"] ["c" "3"]]
>> sqlite %test.db "SELECT * FROM TABLE"
== [["a" "1"] ["b" "2"] ["c" "3"]]
>> sqlcmd sn db "SELECT * FROM TABLE"
== [["a" "1"] ["b" "2"] ["c" "3"]]

manipulating data:

>> blk: [["a" "1"]["b" "2"]["c" "3"]]
>> munge/update blk [2 integer!]
== [["a" 1] ["b" 2] ["c" 3]]
>> munge/delete/where blk "b"
== [["a" 1] ["c" 3]]

joining data:

>> staff: [["Joe" 0]["Bob" 1]["Ben" 0]]
>> type: [[0 "Permanent"][1 "Contractor"]]
>> merge staff 2 type 1 [1 4]          
== [["Joe" "Permanent"] ["Bob" "Contractor"] ["Ben" "Permanent"]]

querying data:

>> munge/part [["Joe" 21]["Bob" 33]["Ben" 20]] 1
== [["Joe"] ["Bob"] ["Ben"]]
>> munge/where [["Joe" 21]["Bob" 33]["Ben" 20]] [row/2 > 30]
== [["Bob" 33]]
>> munge/group [["Joe"]["Bob"]["Ben"]["Bob"]] 'count
== [["Ben" 1] ["Bob" 2] ["Joe" 1]]
>> munge/group [["Joe"]["Bob"]["Ben"]["Bob"]] [count > 1]
== [["Bob" 2]]

and saving data:

>> write-dsv %comma.csv [["a" "1"] ["b" "2"] ["c" "3"]]
>> write-dsv %tabs.txt [["a" "1"] ["b" "2"] ["c" "3"]]
>> write-excel %xml.xlsx ["A" [[A B] [1 2]] [5 10]]