Return Arrays/ Linked Lists

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 pusztry.

I am fairly new to programing and this may be an easy question. I was ondering if you can return an array or linked list from a procedure? If you can, Can you give me a quick example of how to do this. I have tried a few things and can't seem to get it to work. Here is a quick example of what I am tring to do.

Code: Select all

 Procedure.s parseString(buffer$.s, parseCharacter.s)
  NewList results.s()

  buffer$ = "Data1,Data2,Data3,Data4,"
  lEnd.l = FindString(buffer$, parseCharacter, 1)

  Debug "The starting string is:"
  Debug buffer$

  Repeat
    If lEnd > 0     ;If > 0 then we have a parseCharacter , in the buffer
      i = 0
      sData$ = Left(buffer$, lEnd - 1)  ; sData will be all characters before lEnd
      buffer$ = Mid(buffer$, lEnd + 1, Len(buffer$))  ; We want To delete the Data that we just
                                                      ; got from the buffer including the ,
      Debug sData$                                    ; Display the Data
      lEnd = FindString(buffer$, parseCharacter, 1)          ; Gets Starting position of the parseCharacter in the new buffer
      i = i + 1
    EndIf 
  Until lEnd 
WinXP, PIII 800 MHz, 512MB RAM, SB Live 5.1, NVidia TNT 2 Ultra
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 El_Choni.

You can't return an array or linked list, but you can use globals for that. I think linked lists are always global, not so sure about arrays. I hope somebody else can help you further.

Sorry, bye,

El_Choni
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 freak.

Linked lists and Arrays are always Global.
Just define them in the main prog and use them in the procedure.

Timo
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 pusztry.

Thanks for the insite. I also did some further investigation and I can send any type back if I use a 'Structure'. This seems to be the best way for me to keep the procedure self contained. Again thanks. I am going to put your tip in my tips section so that I remember it.

Ryan

Ryan

WinXP, PIII 800 MHz, 512MB RAM, SB Live 5.1, NVidia TNT 2 Ultra
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 pusztry.

Hi I'm back,
Can somebody help me. I am tring to parse a string in a procedure and return an array of the parsed parts of the string. I can't get it to work. I have tried to define an array in the main code and use it in the procedure but it gives me an error that it isn't an array or linked list. I have tried to pass it an array and I can't get that to work. I am at a loss. I am new to PB and I can't firgure out how to do this. Can I see an example of this? It would help me out so much.

Thanks

Ryan

WinXP, PIII 800 MHz, 512MB RAM, SB Live 5.1, NVidia TNT 2 Ultra
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 freak.

OK, here we go...

I took your procedure above, an i got it to work.
I've tried to comment it, so it's better to understand.

Code: Select all

Procedure.l parseString(buffer.s, parseCharacter.s)
                        ; first it counts the pieces
  pieces.l = 1          ; if there's no parseCharacter, it's still one piece
  position.l = 0
  Repeat                
    position = position + 1     ; step left one position
    position = FindString(buffer, parseCharacter, position)  ; get position of next parseChar starting from position
    If position  0
      pieces = pieces + 1   ; found anotherr piece
    EndIf
  Until position = 0  ; no more parseChars
    
  Dim results.s(pieces)  ; define the Array with the size of pieces.
                         ; doesn't have to be in the main prog. this works as well.
                         ; note that results() is resized every time this procedure is called.
    
  i = 1    ; for the piece number
  Repeat
    position = FindString(buffer, parseCharacter, 1)  ; get position of parseChar
    If position  0    
      results(i) = Left(buffer, position - 1) ; get result (starting of buffer)
      buffer = Right(buffer, Len(buffer) - position)  ; cut found stuff from buffer
      i = i + 1     ; next piece
    EndIf
  Until position = 0  ; no more parseChars in the buffer
  results(pieces) = buffer  ; the last piece has to be retuned as well
   
  ProcedureReturn pieces  ; returns the number of pieces, the pieces are stored in results()
EndProcedure
  
  
; --- Test ---
  
  
number.l = parseString("Data1,Data2,Data3,Data4",",")  ; calling the procedure
  
For i = 1 To number   
  Debug results(i)   ; displays the results
Next
End
Arrays can be redefined using a variable, and they are always Global.
Arrays are not a big deal, you just have to know how to use them.

I hope the code works for you, and you are able to learn a bit from it...

Timo

--

A debugged program is one for which you have not yet found the conditions that make it fail.
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 pusztry.

I can't thank you enough. It was great that you got back to me so fast. I haven't got to digest it yet but it does exactly what I wanted. THANK YOU.


Ryan

WinXP, PIII 800 MHz, 512MB RAM, SB Live 5.1, NVidia TNT 2 Ultra
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 freak.

no problem
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 pusztry.

Is there a way to see the size of the array? I tried the "Len" command and it doesn't work. Is there a way that I can call arrays as variables?

Thanks in advanced
Ryan

Ryan

WinXP, PIII 800 MHz, 512MB RAM, SB Live 5.1, NVidia TNT 2 Ultra
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 freak.

Do you mean, how much entrys it has? Or the length of the strings in the array?

The number of entrys is returned by the procedure.

Len() should work with arrays. You can do pretty much everything you can do with
variables as well.

You just have to specify the entry like this:

length = Len(results(3))

I hope i understood you right...

Timo

--

A debugged program is one for which you have not yet found the conditions that make it fail.
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 pusztry.

I did mean how much entrys it has. How could I do it without the procedure?

Thanks for taking time to answer my easy question.

Ryan

WinXP, PIII 800 MHz, 512MB RAM, SB Live 5.1, NVidia TNT 2 Ultra
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 freak.

Well, not that easy kind of a Question.

It's not possible for now. There's no PB command for that.

But do you really need that? You could also do it like this:

Code: Select all

Global ArraySize.l  ; define a global Variable at Start.
  
; now if you size the Array.
Dim Array.w(somesize)
ArraySize = somesize
This way, you'll always have the size of the Array stored in ArraySize, and
since it's global, you can access it everywhere in your program.

Not the best solution, I know, but it does the job.

Timo
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 pusztry.

Thanks very much. I have been looking over your example and it is great. I haope that a function that returns the amount of item loaded in an array will be added to the PureBasic language. But your solution will work.

Thanks

Ryan

WinXP, PIII 800 MHz, 512MB RAM, SB Live 5.1, NVidia TNT 2 Ultra
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 PB.

> I haope that a function that returns the amount of item loaded in an array
> will be added to the PureBasic language.

The procedure above already does that... the variable "pieces" holds the number
of elements of the array, since the procedure itself put them there with "Dim".

Arrays can only ever be created by the user, and thus you should always already
know how many elements are in them. No need for a command at all. I'd like to
see a situation where such a command is needed.


PB - Registered PureBasic Coder

Edited by - PB on 31 July 2002 14:39:40
Post Reply