Simple list example???

Just starting out? Need help? Post your questions and find answers here.
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by Fangbeast.

I'm lost again (Purebasic is harder for me than powerbasic)!!

I need a simple linked list example to get my head around it..

1. define a linked list. (say around 10 elements)
2. load a list with string data
3. search elements of the list
4. replace elemnts of the list with changed data
5. print the elements to the screen

Anyone have some simple examples??

Fangles
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by Mr.Skunk.

Hi,

Here is an example:

Code: Select all

 
;===
; Create a NewList
;===


Structure somestrings ; Define a structure for the list
  Name.s
  Message.s
EndStructure


NewList MyList.somestrings () ; Create the list with the structure pattern


For i=1 To 5
  Read a$
  Read b$
  AddElement(MyList()) ; Lists never use indexes like arrays
  MyList()\Name=a$ ; Fill the list using the structure pattern
  MyList()\Message=b$
Next i


DataSection
Data.s "name1","message1","name2","message2","name3","message3","name4","message4","name5","message5"
EndDataSection


;===
; search/replace elements of the list
;===


ResetList(MyList()) ; Place pointer before the first element, so the first element can be seen with NextElement()
While NextElement(MyList()) ; Browse all the list elements
  If MyList()\Name="name4"
    MyList()\Name="Your Name" ; Replace the element
  EndIf
 Wend
 

;===
; Print elements
;===


If OpenWindow(0, 100, 100, 195, 260, #PB_Window_SystemMenu | #PB_Window_MinimizeGadget | #PB_Window_MaximizeGadget, "PureBasic Window")

  DrawingOutput(WindowID())
  j=0
  ResetList(MyList())
 While NextElement(MyList())
   Locate(20,20+(j*20)):DrawText(MyList()\Name+" : "+MyList()\Message)
   j+1
 Wend
 StopDrawing()
 

 Repeat
   ev.l=WaitWindowEvent()
 Until ev.l=#PB_EventCloseWindow
EndIf
Hope it helps

Mr Skunk

Mr Skunk's PureBasic Web Page
http://www.skunknet.fr.st
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by Fangbeast.

Thanks Mr Skunk, it helps a lot. Pity there is no index feature though. I'm going to have to do some fancy footwork witht he log file but I'm beginning to see.

Can I do string replace/checking on a list element directly?
I.e
ListLen = len(MyList()\Name)
If left(MyList()\Name, 5) = "name4" then
MyList()\Name = Mid(MyList()\Name, 6, ListLen - 5))

or should I assign the element to a temporary string first, do the job and then reassign it for safety?

I'kk go and play but thought that I'd ask.

Fangles
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by Mr.Skunk.

Hi

I don't think it's a pity.
It's great to have no index in lists.
You can use array (dim) if you want to use index, but lists are much more powerfull...

You can access a List element like any other variable within PB. (No need to have temporary string)

Mr Skunk

Mr Skunk's PureBasic Web Page
http://www.skunknet.fr.st

Edited by - mr.skunk on 10 October 2001 05:38:27
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by Fangbeast.

I have to disagree with you there. Having been shown how powerful lists can be, people will be making incredibly large lists. And since list elements can be of any size AND we have to go through them ONE AT A TIME (sequentially) to find wanted data, this can take time, even on a fast computer.

Ah well, back to the salt mines. What works one day is guaranteed not to work the nex...Old Microsoft Motto

Fang

Fangles
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by Rings.

Your absolutly right Fangles !
(And those who did not understand the words did not code pro-applications)


Siggi
Post Reply