-
Notifications
You must be signed in to change notification settings - Fork 41
ARRAY e JSONString #239
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
M is a map variable. As shown in the user guide, you can access the elements of the map variable using either dot-notation or brackets:
See section "Add Key-Value Pairs" to add more elements to the map. Maybe for your use-case it is better to use an array:
|
Hello, Thanks for the quick reply! Since JSON strings were not mentioned, I thought it would be better to write a clearer explanation about maps (without discussing nesting for now). Here's an example: `'creating map by assignment 'creating map by ARRAY (function?) |
I'll add some better explanation to the guide. To save maps to a file as json use TSAVE. See examples for TLOAD for save and load. To convert a map to string (json) use STR. You can then save and load the string with standard file-io. |
Olá, Estou trabalhando em um projeto em SmallBASIC onde preciso armazenar dados em um formato similar ao JSON, mas estou tendo dificuldades em garantir que o arquivo gerado seja adequadamente formatado (com indentação e chaves bem estruturadas). Atualmente, estou usando tload e tsave, mas esses métodos não geram um formato legível de JSON. O que eu estou tentando fazer: O que já fiz: `meu_map = {} if exist("meu_map.json") then sub inserir() repeat repeat repeat meu_array << meu_map sub exibir() for i in meu_array print "-------------------------" INPUT "Quantos registros? ", num_lin A minha dúvida: Como posso modificar o código para gerar um arquivo mais legível e no formato correto, com indentação, e conseguir carregar os dados de volta corretamente? |
I think SmallBASIC can't do this kind of formatted json output . I googled a little bit and was surprised that there exist many json formatting tools. You can paste an one-line json string and these tools will convert it to something nicely readable. I'm afraid you need to write this kind of tool for SmallBASIC. I would convert the map using STR to a string and then go character by character through that string and write the character to a file (or new string). Using some if-then, like if(c == ",") then insert new line + spaces. To read the nicely formatted data back, I would tload the file. Every element of the resulting array is a line of the json structure as a string. Take all these strings and concatenate them (using i.e. JOIN). |
Olá, por ora resolvi adotar o formato similar a JSON, que chamei anteriormente de "json-like". Ao estudar a sintaxe de SmallBASIC, desenvolvi um script e o publiquei em https://computerlanguagesite.wordpress.com/2025/03/16/smallbasic-tabela-com-units-e-map/. Se puder e quiser, por favor, dê uma olhada e eventuais sugestões. Obrigado! |
Thank you for sharing your program. Looks great. I don't have anything to complain. Maybe you can assign the ID automatically. Your program can check for the highest ID used so far, and then increase the ID with every new dataset. |
Olá!
Sou novato nessa linguagem e não entendi esta seção do SmallBASIC User Guide:
By ARRAY
Using the command M = ARRAY(JSONString) will create a map variable from the string JSONString.
M = ARRAY("{x:1, y:2, z:3}")
s = "{1:10, 2:5, Name:"SmallBASIC"}" ' The quotation marks of the string SmallBASIC are escaped with "
M = ARRAY(s)
A letra variável M pode ter outro identificador, não? E se refere a um array M cujos elementos poderiam ser acessados via M( índice ) ou M[ índice ], por exemplo? Não entendo muito bem, mas se a string é imutável, estática, então como posso usar esse conceito em um exemplo dinâmico? Podem ilustrar com alguns exemplos, por favor.
At.te,
Luiz
The text was updated successfully, but these errors were encountered: