REBOL [
    Title: "Result-set to lookup table"
    Purpose: {Given an SQL result set, which comes in the form of
    a block of blocks, generate a lookup table where the key is
    the first item of a row and the attribute for the key is a
    block containing the remaining items from the row.}
]

;; [---------------------------------------------------------------------------]
;; [ This is a specialized function for a specific project.  It starts with    ]
;; [ the result set of an SQL query, which is a block of blocks, where each    ]
;; [ sub-block is a row of the result set.  It transforms that data into a     ]
;; [ lookup table where the key to the table is the first item of a row,       ]
;; [ and the attribute of the key is a block containing all the remaining      ]
;; [ items in the row.                                                         ]
;; [---------------------------------------------------------------------------]

LOOKUP-TABLE-OF-RESULT-SET: func [
    ROWBLOCK
    /local LOOKUPTABLE ATTRBLK ROWLGH ROWSUB 
] [
    LOOKUPTABLE: copy []
    foreach ROW ROWBLOCK [
        ROWLGH: length? ROW
        append  LOOKUPTABLE first ROW
        ROWSUB: 2
        ATTRBLK: copy []
        loop (ROWLGH - 1) [
            append ATTRBLK pick ROW ROWSUB
            ROWSUB: ROWSUB + 1
        ]
        append/only LOOKUPTABLE ATTRBLK
    ]
    return LOOKUPTABLE
]

;;Uncomment to test
;TBL1: LOOKUP-TABLE-OF-RESULT-SET [
;    ["AAAA1" "BBBB1" "CCCC1"]
;    ["AAAA2" "BBBB2" "CCCC2"]
;    ["AAAA3" "BBBB3" "CCCC3"]
;    ["AAAA4" "BBBB4" "CCCC4"]
;    ["AAAA5" "BBBB5" "CCCC5"]
;]
;probe TBL1
;halt