AddElement in Structured List

Just starting out? Need help? Post your questions and find answers here.
User avatar
Janni
Enthusiast
Enthusiast
Posts: 127
Joined: Mon Feb 21, 2022 5:58 pm
Location: Norway

AddElement in Structured List

Post by Janni »

Hi,

What basics am I missing here ?
It fetches line 1, 6, 11,16.
If I only have one fila() it fetches all lines, but then I don't see how to get column 1 in date, column 2 in id etc. -from each line in the txt file.

Code: Select all

Structure logfile
  date$
  id$
  num$
  nicks$
  chat$
EndStructure

OpenConsole()

NewList fila.logfile()

If ReadFile(#file, file$, #PB_Ascii)
  While Eof(#file) = #False 
    
    AddElement(fila())
    fila()\date$ = StringField(ReadString(#file), 1, ";")
    fila()\id$ = StringField(ReadString(#file), 2, ";")
    fila()\num$ = StringField(ReadString(#file), 3, ";")
    fila()\nicks$ = StringField(ReadString(#file), 4, ";")
    fila()\chat$ = StringField(ReadString(#file), 5, ";")
    
   Wend
  CloseFile(#file)
EndIf
Last edited by Janni on Wed Aug 03, 2022 9:41 pm, edited 1 time in total.
Spec: Linux Mint 20.3 Cinnamon, i7-3770K, 16GB RAM, RTX 2070 Super
User avatar
jacdelad
Addict
Addict
Posts: 1473
Joined: Wed Feb 03, 2021 12:46 pm
Location: Planet Riesa
Contact:

Re: AddElement in Structured List

Post by jacdelad »

You read another line each time ReadString() is called. Put ReadString() right after the head of the loop and store the result into a string variable. Then use Stringfield () with the variable:

Code: Select all

Structure logfile
  date$
  id$
  num$
  nicks$
  chat$
EndStructure

OpenConsole()

NewList fila.logfile()

If ReadFile(#file, file$, #PB_Ascii)
  While Eof(#file) = #False 
    Temp$=ReadString(#file)
    AddElement(fila())
    fila()\date$ = StringField(Temp$, 1, ";")
    fila()\id$ = StringField(Temp$, 2, ";")
    fila()\num$ = StringField(Temp$, 3, ";")
    fila()\nicks$ = StringField(Temp$, 4, ";")
    fila()\chat$ = StringField(Temp$, 5, ";")
    
   Wend
  CloseFile(#file)
EndIf
PureBasic 6.04/XProfan X4a/Embarcadero RAD Studio 11/Perl 5.2/Python 3.10
Windows 11/Ryzen 5800X/32GB RAM/Radeon 7770 OC/3TB SSD/11TB HDD
Synology DS1821+/36GB RAM/130TB
Synology DS920+/20GB RAM/54TB
Synology DS916+ii/8GB RAM/12TB
User avatar
Janni
Enthusiast
Enthusiast
Posts: 127
Joined: Mon Feb 21, 2022 5:58 pm
Location: Norway

Re: AddElement in Structured List

Post by Janni »

I clearly wasn't aware that ReadString() read another line each time it's called.
Your solution makes a lot of sense now.

Much appreciated jacdelad, thanks!
Last edited by Janni on Thu Aug 04, 2022 7:49 am, edited 1 time in total.
Spec: Linux Mint 20.3 Cinnamon, i7-3770K, 16GB RAM, RTX 2070 Super
User avatar
jacdelad
Addict
Addict
Posts: 1473
Joined: Wed Feb 03, 2021 12:46 pm
Location: Planet Riesa
Contact:

Re: AddElement in Structured List

Post by jacdelad »

Yay, I'm helpful!
PureBasic 6.04/XProfan X4a/Embarcadero RAD Studio 11/Perl 5.2/Python 3.10
Windows 11/Ryzen 5800X/32GB RAM/Radeon 7770 OC/3TB SSD/11TB HDD
Synology DS1821+/36GB RAM/130TB
Synology DS920+/20GB RAM/54TB
Synology DS916+ii/8GB RAM/12TB
Post Reply