Page 1 of 1

Reading strings into an array

Posted: Wed Jan 11, 2017 9:55 am
by smacker
anyway, I have a For - Next loop that makes six passes and digs out strings from something else for a total of six strings but I want to make sure that a string is only put in the array once and there are no duplicates in the array. So, how do I place strings into an array yet ensure a duplicate of an already existing array string is not also placed into the array?

Also, which would be better to use - an array or List?

Re: Reading strings into an array

Posted: Wed Jan 11, 2017 10:06 am
by Josh
smacker wrote:Also, which would be better to use - an array or List?
Did you have a look to Maps?

Re: Reading strings into an array

Posted: Wed Jan 11, 2017 10:23 am
by infratec
:?: :?: :?:

You have to check if this entry already exists.
Where is the problem?

Bernd

Re: Reading strings into an array

Posted: Wed Jan 11, 2017 10:46 am
by SparrowhawkMMU
I don't think that PureBasic has an InArray() function as exists in some other languages (I could be wrong, I am a relative newcomer to PB)

However, here is a quick and dirty solution - it works fine on small arrays but larger arrays would not scale so well. I am sure veteran PBers will have a more elegant solution, but if you are currently stuck, this should work for you for now:

Code: Select all

EnableExplicit

Procedure.b InArray(Array myArray.s(1), myValue.s)
	
	Protected count.i   = 0
	Protected inArray.b = #False
	
	For count = 0 To ArraySize(myArray()) - 1
		If myArray(count) = myValue
			inArray = #True
			Break
		EndIf
	Next count
	
	ProcedureReturn inArray
EndProcedure

Dim target.s(6)

Dim colours.s(6)

colours(0) = "red"
colours(1) = "green"
colours(2) = "yellow"
colours(3) = "red"
colours(4) = "blue"
colours(5) = "green"

Define count.i = 0
Define index.i = 0

OpenConsole("Array Test")

For count = 0 To 5
	Print("Checking for " + colours(count) + " ... ")
	If InArray(target(), colours(count))
		PrintN("ALREADY IN ARRAY")
	Else
		target(index) = colours(count)
		index + 1
		PrintN("ADDED TO ARRAY")
	EndIf	
Next

PrintN(#CRLF$ + "TARGET ARRAY CONTENTS:")
For count = 0 To 5
	PrintN("INDEX " + count + " = " + target(count))
Next

CloseConsole()

End
Gives this output:

Code: Select all

Checking for red ... ADDED TO ARRAY
Checking for green ... ADDED TO ARRAY
Checking for yellow ... ADDED TO ARRAY
Checking for red ... ALREADY IN ARRAY
Checking for blue ... ADDED TO ARRAY
Checking for green ... ALREADY IN ARRAY

TARGET ARRAY CONTENTS:
INDEX 0 = red
INDEX 1 = green
INDEX 2 = yellow
INDEX 3 = blue
INDEX 4 = 
INDEX 5 = 
Hope this helps.

Re: Reading strings into an array

Posted: Wed Jan 11, 2017 2:43 pm
by ebs
To follow up on Josh's suggestion, here's what I often do using a Map:

Code: Select all

Global NewMap Strings.s()

Procedure AddString(String.s)
  Strings(MD5Fingerprint(@String, StringByteLength(String))) = String
EndProcedure

String1.s = "This is the first string"
String2.s = "This is the second string"
String3.s = "This is the first string"
String4.s = "This is the fourth string"

AddString(String1)
AddString(String2)
AddString(String3)  ; won't duplicate String1
AddString(String4)

ForEach Strings()
  Debug Strings()
Next
Note that this won't work if the order of the strings is important; Maps don't keep the order of inserted items.

Regards,
Eric

Re: Reading strings into an array

Posted: Wed Jan 11, 2017 7:46 pm
by smacker
@ Josh & ebs

Did not even think about Maps. It works fine though, however, I ended up going with the array after all. When I expand this thing later i'll probably move to Maps instead. Thanks for your excellent suggestions and replies :)

@ SparrowhawkMMU

Yeah, I sort of miss an 'InArray(..)' function also and that's part of what I was basically trying to figure out in PureBasic also. Your reply filled the need wonderfully and worked great, thanks :)

Re: Reading strings into an array

Posted: Thu Jan 12, 2017 9:44 am
by SparrowhawkMMU
@smacker - glad I could help. :)