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?
Reading strings into an array
Reading strings into an array
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.
Re: Reading strings into an array
Did you have a look to Maps?smacker wrote:Also, which would be better to use - an array or List?
sorry for my bad english
Re: Reading strings into an array
You have to check if this entry already exists.
Where is the problem?
Bernd
- SparrowhawkMMU
- User
- Posts: 44
- Joined: Fri Jan 17, 2014 8:55 pm
- Location: UK
Re: Reading strings into an array
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:
Gives this output:
Hope this helps.
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
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 =
Last edited by SparrowhawkMMU on Thu Jan 12, 2017 9:42 am, edited 2 times in total.
Re: Reading strings into an array
To follow up on Josh's suggestion, here's what I often do using a Map: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
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
Regards,
Eric
Re: Reading strings into an array
@ 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
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.
- SparrowhawkMMU
- User
- Posts: 44
- Joined: Fri Jan 17, 2014 8:55 pm
- Location: UK
Re: Reading strings into an array
@smacker - glad I could help.