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:

>> munge %test.csv 2
== ["a" "1" "b" "2" "c" "3"]
>> munge %test.xlsx 2
== ["a" "1" "b" "2" "c" "3"]
>> munge sqlcmd sn db "select * from table" 2
== ["a" "1" "b" "2" "c" "3"]

manipulating data:

>> munge/delete/where [a 1 b 2 c 3] 2 [c1 = 'b]
== [a 1 c 3]
>> munge/update [a 1 b 2 c 3] 2 [2 0]
== [a 0 b 0 c 0]
>> munge/update ["a" "1" "b" "2" "c" "3"] 2 [2 integer!]
== ["a" 1 "b" 2 "c" 3]
>> munge/update/where [a 1 b 2 c 3] 2 [2 0] [c1 = 'b]
== [a 1 b 0 c 3]

joining data:

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

querying data:

>> munge/where ["Joe" 21 "Bob" 33 "Ben" 20] [Name Age] [Age > 30]
== ["Bob" 33]
>> munge/where/headings ["Joe" 21 "Bob" 33 "Ben" 20] [Name Age] [Age > 30]
== [Name Age "Bob" 33]
>> munge/group [a b a b c d] 2 'count
== [a b 2 c d 1]
>> munge/group [a b a b c d] 2 [count > 1]
== [a b 2]
>> munge/group [a 1 a 2 c 3 c 4] 2 'sum
== [a 3 c 7]
>> munge/part [a 1 b 2 c 3] 2 1
== [a b c]
>> munge/where [a 1 b 2 c 3] 2 [odd? c2]
== [a 1 c 3]
>> munge/where [a 1 b 2 c 3] 2 3
== [c 3]
>> munge/order [b 1 a 2] 2
== [a 2 b 1]

and saving data:

>> munge/save [a 1 b 2 c 3] 2 %test.csv
>> munge/save [a 1 b 2 c 3] 2 %test.txt
>> write-excel %test.xlsx [
    "Test" ["Response" "Count" "Yes" 10 "No" 5 "" "=SUM(B2:B3)"] [20 10]
]