Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
search array for dupes add item if not found
#1
If I have a dialog with a combo box which is populated with items loaded from a text file, (one item per line in the text file)
an array is created from this file.

The user has the option to select an item from the drop down list presented by the combo box or can enter their own value into the list box.

If the user enters a value of their own into the combo box, what would be the best way to check that this is not already in the array and if it is not present add it to the array.

Thanks in advance for any help.
#2
Function ARRAY_str_AddUnique
Code:
Copy      Help
;/
function# ARRAY(str)&a $s [flags] ;;flags: 1 case insensitive

;Adds string s to array a. Does not add if a already contains the string.
;Returns index of found or new element.


int i ci(flags&1)
for i 0 a.len
,if(!StrCompare(a[i] s ci)) ret i
a[]=s
ret a.ubound
#3
Thank you for that Gintaras, that reply was almost immediate.

If I may ask for a little more advice.

In my proposed macro. I have created an array from my text file and named the array a1.
I will assign the array to a combobox with :

ARRAY(str) a1=cb3Sit
a1.sort(2)
cb3Sit=a1

using your function how would I call it to check the entered value in cb3Sit against a1 then write a1 back to a file.

Please forgive me if this is simple, I am still learning
#4
Disregard that last Post Gintaras, I have worked out out how to use your function and save the array.
#5
This works just perfect however how do I remove the -1 from the front of the output.
#6
ComboBox variables begin with selected item index, like "-1 text". To get text, can be used str.gett or some other string function.

cb3Sit.gett(cb3Sit 1)
#7
Thank you Gintaras problem solved.

I still have loads to learn.
#8
Better use TO_CBGetItem, not str.gett, because gett may get only part of text.

TO_CBGetItem cb3Sit
#9
Works perfectly now Gintaras. My apologies though, I am sure you have already explained the use of TO_CBGetItem to me in the past.
I remembered as soon as I saw your last post.

Thanks again for your help. Big Grin
#10
Good morning Gintaras,

Sorry to bother you again. I have studied the workings of your function, however there are a couple of items I am not clear about.
could you explain the function of ci and the use of (flags&1). I have searched through the help file but am none the wiser for it.
#11
I can see that you have somehow assigned (flags&1) i.e case insensitive to the variable ci, but I don't fully understand how this works.
Is there anywhere in the help file you could point me to
#12
& is an operator. Look in QM Help, in Operators topic. It is used in many programming languages, you can find more info in Wikipedia etc. Often used to test if a flag exists in an integer variable. If flag 1 exists in flags, ci is 1, else ci is 0.
#13
Thank you for that Gintaras, I understand now.


Forum Jump:


Users browsing this thread: 1 Guest(s)