Bookmark and Share

Notice: On April 23, 2014, Statalist moved from an email list to a forum, based at statalist.org.


[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: st: Save asarray


From   Phil Schumm <[email protected]>
To   Statalist Statalist <[email protected]>
Subject   Re: st: Save asarray
Date   Sat, 18 Jan 2014 09:18:51 -0600

On Jan 17, 2014, at 6:29 PM, Sergiy Radyakin <[email protected]> wrote:
> I am interested in preserving an associative array between Stata sessions.
> 
> There does not seem to be a command that would save/load the whole array as one binary object into a file, or I've failed to find one.


The function fputmatrix() will save an associative array, which can then be read by fgetmatrix(), e.g.:

    A = asarray_create()
    asarray(A, "name", "Joe Smith")
    asarray(A, "age", 22)
    asarray(A, "address", "123 Main St.")

    fh1 = fopen("test.dat", "w")
    fputmatrix(fh1, A)
    fclose(fh1)

    fh2 = fopen("test.dat", "r")
    B = fgetmatrix(fh2)
    asarray_keys(B)
    asarray(B, asarray_keys(B)[1])
    asarray(B, asarray_keys(B)[2])
    asarray(B, asarray_keys(B)[3])
    fclose(fh2)

Unfortunately, there is no way to write a class instance to a file.  Thus, I often store data members in one or more associative arrays and write these instead, reconstructing the instance on the fly when I read them back in.


-- Phil


*
*   For searches and help try:
*   http://www.stata.com/help.cgi?search
*   http://www.stata.com/support/faqs/resources/statalist-faq/
*   http://www.ats.ucla.edu/stat/stata/


© Copyright 1996–2018 StataCorp LLC   |   Terms of use   |   Privacy   |   Contact us   |   Site index