Quickest way to input up to 10 strings into a structure ?

Just starting out? Need help? Post your questions and find answers here.
User avatar
Primoz128
Enthusiast
Enthusiast
Posts: 212
Joined: Sat Sep 10, 2011 8:25 pm
Location: Slovenia

Quickest way to input up to 10 strings into a structure ?

Post by Primoz128 »

Well i am gona use a structure which is gona have a map(1 key) and a list with up to 10 elements.

But now i don't know what would be the quickest way to input up to 10 elements into the list... trough procedure it would require a lot of arguments... that isn't viable if you ask me.

Maybe "Data" ? Though i got no idea how to use it...
User avatar
spikey
Enthusiast
Enthusiast
Posts: 771
Joined: Wed Sep 22, 2010 1:17 pm
Location: United Kingdom

Re: Quickest way to input up to 10 strings into a structure

Post by spikey »

davido
Addict
Addict
Posts: 1890
Joined: Fri Nov 09, 2012 11:04 pm
Location: Uttoxeter, UK

Re: Quickest way to input up to 10 strings into a structure

Post by davido »

Hi spikey:

Nice example.
I read the manual a lot; must have missed this one.
DE AA EB
User avatar
TI-994A
Addict
Addict
Posts: 2741
Joined: Sat Feb 19, 2011 3:47 am
Location: Singapore
Contact:

Re: Quickest way to input up to 10 strings into a structure

Post by TI-994A »

Primoz128 wrote:...what would be the quickest way to input up to 10 elements into the list... trough procedure it would require a lot of arguments... Maybe "Data" ?
Hello Primoz128. Please take a look at these examples. The first one populates a LinkedList with elements read from a data block directly, while the second encapsulates the process in a reusable procedure:

Code: Select all

NewList scientists.s()

Restore scientists
For readData = 1 To 8
  Read.s name$
  AddElement(scientists())
  scientists() = name$
Next readData

ResetList(scientists())
While NextElement(scientists())
  Debug scientists()
Wend

DataSection
  scientists:    
  Data.s "Albert Einstein", "Isaac Newton", "Charles Darwin", "Guglielmo Marconi"
  Data.s "Galileo Galilei", "Leonardo da Vinci", "René Descartes", "Thomas Edison" 
EndDataSection

Code: Select all

Procedure asmRestore(address)
  !MOV EAX, [ESP+PS0]
  !MOV [PB_DataPointer],EAX
EndProcedure

Procedure fillList(List emptyList.s(), dataLabel.i, elements.i)
  asmRestore(dataLabel)
  For readData = 1 To elements
    Read.s name$
    AddElement(emptyList())
    emptyList() = name$
  Next readData
EndProcedure

NewList scientists.s()
fillList(scientists(), ?scientists, 8)

ResetList(scientists())
While NextElement(scientists())
  Debug scientists()
Wend

DataSection
  scientists:    
  Data.s "Albert Einstein", "Isaac Newton", "Charles Darwin", "Guglielmo Marconi"
  Data.s "Galileo Galilei", "Leonardo da Vinci", "René Descartes", "Thomas Edison" 
EndDataSection
We have to thank srod for the asmRestore() routine that facilitates the restoring of data blocks through label addresses. Hope it helps.
Texas Instruments TI-99/4A Home Computer: the first home computer with a 16bit processor, crammed into an 8bit architecture. Great hardware - Poor design - Wonderful BASIC engine. And it could talk too! Please visit my YouTube Channel :D
User avatar
luis
Addict
Addict
Posts: 3895
Joined: Wed Aug 31, 2005 11:09 pm
Location: Italy

Re: Quickest way to input up to 10 strings into a structure

Post by luis »

TI-99, that asmRestore is wrong for a general use because uses PS0.
That's a constant defined in the ASM code generated by PB, it's "PS" + a number and differs for every procedures. So PS0 is valid only if asmRestore it's the first procedure defined.
Moreover its value is dependent by the number and type of parameters of the procedure, so if it works when it's not the first procedure it's just by chance (the PS0 constant of another procedure has by chance the right value to access the right param in the asmRestore proc).

You can use this one instead:

Code: Select all

Procedure RestoreEx (*address) 
; [DESC]
; Restore the data pointer using a variable instead of a label.
; 
; [INPUT]
; *address : A pointer inside the data section.
;
; [RETURN]
; NONE
; 
; [NOTES]
; Example:
;
; DataSection
;  Data.i 10, 20, 30
; label_test:
;  Data.i 40, 50, 60
;  Data.i 70, 80, 90
; EndDataSection
; 
; *ptr = ?label_test
; RestoreEx (*ptr) 
; Read.i a
; Debug a ; 40
 
 CompilerIf (#PB_Compiler_Processor = #PB_Processor_x86)
  !mov eax, [p.p_address]
  !mov [PB_DataPointer], eax
  CompilerElse   
  !mov rax, [p.p_address]
  !mov [PB_DataPointer], rax  
 CompilerEndIf
EndProcedure
"Have you tried turning it off and on again ?"
bosker
Enthusiast
Enthusiast
Posts: 105
Joined: Fri Jan 08, 2010 11:04 pm
Location: Hampshire, UK

Re: Quickest way to input up to 10 strings into a structure

Post by bosker »

Or even...
No restore and no assembler.

Code: Select all

EnableExplicit

Procedure fillList(List emptyList.s(), dataLabel.i)
    Protected *StrAdr.String

    Repeat
        *StrAdr = datalabel         ; get string pointer address

        If Len(*StrAdr\s)           ; end of list marked by empty string
            AddElement(EmptyList())
            EmptyList() = *StrAdr\s
            datalabel + SizeOf(Integer)
        EndIf
    Until Len(*StrAdr\s) = 0
EndProcedure

NewList scientists.s()
fillList(scientists(), ?scientists)

ResetList(scientists())

While NextElement(scientists())
    Debug scientists()
Wend

; Data is declared as string pointers rather than strings
DataSection
scientists:
    Data.i @"Albert Einstein", @"Isaac Newton", @"Charles Darwin", @"Guglielmo Marconi"
    Data.i @"Galileo Galilei", @"Leonardo da Vinci", @"René Descartes", @"Thomas Edison"
    Data.i @""      ; list terminator (so I don't have to count how many)
EndDataSection

User avatar
Primoz128
Enthusiast
Enthusiast
Posts: 212
Joined: Sat Sep 10, 2011 8:25 pm
Location: Slovenia

Re: Quickest way to input up to 10 strings into a structure

Post by Primoz128 »

Thanks to you all. Gona try and implement it soon enough.
Post Reply