Please look my example and help me to understand - Forum

Forum Navigation
You need to log in to create posts and topics.

Please look my example and help me to understand

Hi all.

I have a new problem with retrieve data from a txt file and save it to an arrays .

I show you the example so that you have an idea.

Spanish: Gracias a otros ejemplos que habeís subido he aprendido a recuperar datos de un archivo txt a un listbox, cuando he querido hacer lo mismo con arrays me encuentro con dificultades y me gustaría que vierais mi ejemplo para darme una posible solución. gracias.

Sorry, I can´t upload the txt file. Its would be your data

datafile: data.txt

items: Name1,valorA1,valorB1,Name2,valorA2,valorB2,Name3,valorA3,valorB3,Name4,valorA4,ValorB4,Name5,valorA5,ValorB5

Nota: Upload files son : loadfiletxt-to-arrays

Uploaded files:
  • You need to login to have access to uploads.

@joaquinfdez

Since the populating of ListBox1 works, I will address the rest of your code ... instead of setting up and loading the data file into [myvar], my code below just populates this variable via a SetVar command.

The problem with your technique for attempting to create arrays using ...

- 1st, 4th, 7th etc. items
- 2nd, 5th, 8th etc. items
- 3rd, 6th, 9th etc. items

... is in how you loop through the items ... with your way, with 15 items, the loop has 15 iterations (0 to 14) ... and you are asking for ...

math "[x]*3" 0 [p]

... which can be as high as 42 ... but [myarray] only contains 15 items.

Study this code ...

CreateEmptyArray [rec]
CreateEmptyArray [rec1]
CreateEmptyArray [rec2]

SetVar "[myvar]" "Name1,valorA1,valorB1,Name2,valorA2,valorB2,Name3,valorA3,valorB3,Name4,valorA4,ValorB4,Name5,valorA5,ValorB5"
StrParse "[myvar]" "," [myarray]
ArrayLen [myarray] [totalitems]

Math "[totalitems]/3" "0" "[p]"

SetVar [totalitems] [totalitems]-1
Loop 0 [totalitems] [n]
  ListBoxAddItem "Listbox1" "[myarray([n])]" "[myarray([n])]"
EndLoop
ListBoxDeleteItem "Listbox1" 1

....   Guarda en el array rec solo los valores Name1,Name2.......Name5 saltando de 3 en 3
loop 0 [p] [x]
  math "[x]*3" 0 [n]
  ArrayAddItem "rec" "[myarray([n])]"
endloop

.Modificando loop para empezar desde 1  Guarda en el array rec1 solo los valores ValorA1,ValorA2,ValorA3...ValorA5
loop 0 [p] [x]
  math "([x]*3)+1" 0 [n]
  ArrayAddItem "rec1" "[myarray([n])]"
endloop
 .Modificando loop para empezar desde 2 Guarda en el array rec2 solo los valores ValorB1,ValorB2,ValorB3...ValorB5
loop 0 [p] [x]
  math "([x]*3)+2" 0 [n]
  ArrayAddItem "rec2" "[myarray([n])]"
endloop

... and then adapt it into you button code which populates the array from a file (instead of a SetVar)

luishp and JoaquinFDEZ have reacted to this post.
luishpJoaquinFDEZ

Ok !! Thanks @gaev  I understand now.  I´m very grateful to you

@joaquinfdez adjunto tu ejercicio corregido. No había visto que @gaev habia aportado su solución ya. Fíjate que es importante asignar una variable al ListBox o generará un error. Tampoco es necesario utilizar ya ListBoxAddItem (simplemente asígnale un array al ListBox). Los Loops los he simplificado también.

Cualquier duda, me dices.

Saludos!

Uploaded files:
  • You need to login to have access to uploads.
JoaquinFDEZ has reacted to this post.
JoaquinFDEZ

Ok una vez más mil gracias a ti también @luishp.

Lo estudiaré e intentaré asimilar conceptos para no marearos mucho. :-)