GOLF(2gg)

πŸ“¦ version 601.4.41, in ubuntu questing

πŸ’– GOLF(2gg) β€” the write-array page, but make it make sense πŸ’–

okay bestie, so this is a page from the GOLF manual (section 2gg, development vibes ✨), and it's all about write-array. Yes, lowercase, yes with a dash, that's just how she is. Let's get into it 🫢


🌸 NAME 🌸

write-array β€” (array)

πŸŽ€ PURPOSE πŸŽ€

Store value into an array. That's the whole gig. She puts a thing in a place. Iconic behavior, honestly πŸ’…

✨ SYNTAX ✨

write-array <array> \
    key <key> \
    value <value> \
    [ old-value <old value> ]

see the brackets around old-value? that means it's optional, bestie, you don't have to bring it. but you can 🌈

πŸ¦‹ DESCRIPTION πŸ¦‹

so write-array stores a <value> (that's the "value" clause, keep up 😌) under index <key> (that's the "key" clause) into <array>. and heads up β€” that <array> has to already exist, which means you gotta make it first with new-array. no skipping steps, no cap 🚫

<key> is a number from 0 up to (but not including) the currently allocated array size β€” go peep new-array for that deets. and the type of <value>? that got decided the moment <array> was created (the "type" clause, iykyk), and it can be either a string, a number, or a boolean. pick your fighter ⭐

<key> and <value> together? we call that an "element". cute lil couple, honestly πŸ₯°

the old value sitting under index <key> gets handed back to you in <old value> (that's the "old-value" clause), and then <value> slides right in and replaces the old value. out with the old, in with the new πŸ’œ

okay now the important part, pay attention:

if an <array> was created with the "process-scope" clause (again, see new-array), then that element <value> will not be freed when the current request ends. instead it persists for as long as the process runs β€” unless it gets deleted (see read-array with the delete clause). so yeah, it hangs around. forever-ish. rent free 😭

πŸ”₯ EXAMPLES πŸ”₯

writing data to an array, the classic πŸ’«

new-array arr
write-array arr key 100 value "some data"

writing a new value with the same key index and snagging the previous value (which is "some data") πŸ‘‘

write-array arr key 100 value "new data" old-value od
@Previous value for this key index is <<print-out od>>

the main character example: a whole array service πŸ‘‘

okay so the following is an array service, where a process-scoped array gets created. it does inserting, deleting and querying on indexed keys. the maximum number of keys it holds is 10,000,000 (indexed from 0 to 9,999,999). and a service process like this can run indefinitely β€” she's in it for the long haul, periodt πŸ’…

create the file arrsrv.golf:

%% /arrsrv public
    do-once
    new-array arr max-size 10000000 process-scope type string
    end-do-once

    // Get input parameters
    get-param op
    get-param key
    get-param data
    // Convert string keye to number
    string-number key to key_n

    if-true op equal "add" // Add data to array
        write-array arr key key_n value data old-value old_data
        delete-string old_data
        @Added [<<print-out key>>]
    else-if op equal "delete" // Delete data and obtain the value deleted
        read-array arr key key_n value val delete
        @Deleted [<<print-out val>>]
        delete-string val
    else-if op equal "query" // Query hash based on key value
        read-array arr key key_n value val
        @Value [<<print-out val>>]
    end-if
%%

create and make the application, then run it as a service 🧚

// Create application
gg -k arr
// Make application
gg -q
// Start application (single process key service)
mgrg -w 1 arr

try it from a command line client (see gg) 🌈

// Add data
gg -r --req="/arrsrv/op=add/key=15/data=15" --service --app="/arr" --exec
// Query data
gg -r --req="/arrsrv/op=query/key=15" --service --app="/arr" --exec
// Delete data
gg -r --req="/arrsrv/op=delete/key=15" --service --app="/arr" --exec

see read-array for more examples. she's got receipts πŸ’–


🌟 SEE ALSO 🌟

  • Array

new-array purge-array read-array write-array See all documentation


and that's the whole page, bestie πŸ’– this one came from the GOLF manual, version $VERSION, dated $DATE β€” go forth and write to your arrays responsibly, mwah πŸŽ€βœ¨πŸ¦‹

πŸ“¦ where this page comes from