Студопедия

Главная страница Случайная страница

Разделы сайта

АвтомобилиАстрономияБиологияГеографияДом и садДругие языкиДругоеИнформатикаИсторияКультураЛитератураЛогикаМатематикаМедицинаМеталлургияМеханикаОбразованиеОхрана трудаПедагогикаПолитикаПравоПсихологияРелигияРиторикаСоциологияСпортСтроительствоТехнологияТуризмФизикаФилософияФинансыХимияЧерчениеЭкологияЭкономикаЭлектроника






Saving Lists of Structured Data to a Storage Medium - Serialization






One of the most important things that computing devices allow users to do is save and retrieve data from local and remote storage mediums. To save lists of text data in RFO basic, a useful technique is to " serialize" the info as a long piece of text, with each item in the list separated by a specified text character. To do this, use a FOR loop to cycle through each item in the list, and concatenate each of the items together, with the specified character in between each item in the list. Then save the concatenated text to a file:

list.create s, myuserslist.add myusers, " John Smith", " 123 Tine Ln. Forest Hills NJ", " 555-1234" ~ " Paul Thompson", " 234 Georgetown Pl. Peanut Grove AL", " 555-2345" ~ " Jim Persee", " 345 Pickles Pike Orange Grove FL", " 555-3456" ~ " George Jones", " 456 Topforge Court Mountain Creek CO", " " ~ " Tim Paulson", " ", " 555-5678" list.size myusers, size! Concatenate each item in the " myusers" list together into one long! string, with each item separated 3 pound characters (#): savedata$ = " " % create a new string variable for i = 1 to size % loop through every item list.get myusers, i, m$! Add 3 pound symbols after every item in the list, EXCEPT the last! item: if i = size savedata$ = savedata$ + m$ % concatenate, if last item else savedata$ = savedata$ + m$ + " ###" % concatenate every other item endifnext i! All that concatenated text now exists in the string labeled " savedata$".! Now simply write that string to a file: text.open w, myfile, " myusers.txt" text.writeln myfile, savedata$text.close myfile

To retrieve the data and convert it back to an RFO Basic list structure, follow this pattern:

! First, read the saved text file: text.open r, myfile, " myusers.txt" text.readln myfile, serialdata$text.close myfile! Use the " split" function to separate each stored record from the! loaded text. The character we used to separate those items was! the pound symbols (###). The split function will separate each! item at those specified pound characters, and create an array! holding each of the separated items: split myusers$[], serialdata$, " ###"! Finally, convert the array structure to a list. To do that,! create a new list and add the items from the array created above,! using the list.add.array function (basically this converts an! array to a list, so that the resizing ability of the list! structure can be utilized): list.create s, myuserslist.add.array myusers$[]! Now you can use the list as usual, loop through it, print selected! items, edit specified items, etc.: list.size myusers, sizefor i = 1 to size list.get myusers, i, m$ print m$next i

Serializing data ensures that even multiline text in lists is separated properly and that each entry is stored as an individual item within a monolithic chunk of text. Just be sure that the character(s) you use to separate each data item would never appear in your user data. For example, if your user data fields above were ever to contain 3 pound characters in the example, the split function would incorrecly split the saved text at that point, as separate records. RFO Basic allows the splitting character(s) to be arbitrarily complex. You could, for example, use "! z%l3#a@q_x; " as the split string, which has virtually no likelihood of occuring naturally in your user text.

NOTE: The split function actually accepts a " regex expression" as the split string. Avoid using the single characters ()[]{}.|$^*+? to separate data because those characters have special meaning when creating matching patterns. Regex patterns are a powerful tool for searching text patterns. Lookup " regex tutorial" in Google to learn how they work.






© 2023 :: MyLektsii.ru :: Мои Лекции
Все материалы представленные на сайте исключительно с целью ознакомления читателями и не преследуют коммерческих целей или нарушение авторских прав.
Копирование текстов разрешено только с указанием индексируемой ссылки на источник.