Reading strings into an array

Just starting out? Need help? Post your questions and find answers here.
smacker
User
User
Posts: 55
Joined: Thu Nov 06, 2014 7:18 pm

Reading strings into an array

Post 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?
The world and human nature was screwed up before I was born. It's not my fault and I'm just stuck with trying to deal with the mess left behind, so don't blame me.
User avatar
Josh
Addict
Addict
Posts: 1184
Joined: Sat Feb 13, 2010 3:45 pm

Re: Reading strings into an array

Post by Josh »

smacker wrote:Also, which would be better to use - an array or List?
Did you have a look to Maps?
sorry for my bad english
infratec
Always Here
Always Here
Posts: 5527
Joined: Sun Sep 07, 2008 12:45 pm
Location: Germany

Re: Reading strings into an array

Post by infratec »

:?: :?: :?:

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

Bernd
User avatar
SparrowhawkMMU
User
User
Posts: 44
Joined: Fri Jan 17, 2014 8:55 pm
Location: UK

Re: Reading strings into an array

Post 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.
Last edited by SparrowhawkMMU on Thu Jan 12, 2017 9:42 am, edited 2 times in total.
ebs
Enthusiast
Enthusiast
Posts: 478
Joined: Fri Apr 25, 2003 11:08 pm

Re: Reading strings into an array

Post 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
smacker
User
User
Posts: 55
Joined: Thu Nov 06, 2014 7:18 pm

Re: Reading strings into an array

Post 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 :)
The world and human nature was screwed up before I was born. It's not my fault and I'm just stuck with trying to deal with the mess left behind, so don't blame me.
User avatar
SparrowhawkMMU
User
User
Posts: 44
Joined: Fri Jan 17, 2014 8:55 pm
Location: UK

Re: Reading strings into an array

Post by SparrowhawkMMU »

@smacker - glad I could help. :)
Post Reply