Preventing duplicate items in a stringlist

Hi all

From my testings, "stringlist.Duplicates := dupIgnore/dupError/dupAccept" doesn't appear to help with my problem.

Quick rundown of application:
Two edit fields (Name/IPAddress), 1 button (Add), 1 listbox
When you press the button, it first adds the edName.Text part to the listbox, and then it adds both edName.Text and edIPAddress.Text to a stringlist as a Name=Value pair, which then gets saved to a file.

What I want to do is to prevent doubling up of either the Name or the IPAddress. dupIgnore/Error only works if both Name and IPAddress are the same the second time around, due to it looking at the entire string
(Name=IPAddress instead of Name or IPAddress by themselves).

Is there an easy way to do this, or do I have to figure out a function for myself?

Cheers in advance

Comments

  • : Hi all
    :
    : From my testings, "stringlist.Duplicates := dupIgnore/dupError/dupAccept" doesn't appear to help with my problem.
    :
    : Quick rundown of application:
    : Two edit fields (Name/IPAddress), 1 button (Add), 1 listbox
    : When you press the button, it first adds the edName.Text part to the listbox, and then it adds both edName.Text and edIPAddress.Text to a stringlist as a Name=Value pair, which then gets saved to a file.
    :
    : What I want to do is to prevent doubling up of either the Name or the IPAddress. dupIgnore/Error only works if both Name and IPAddress are the same the second time around, due to it looking at the entire string
    : (Name=IPAddress instead of Name or IPAddress by themselves).
    :
    : Is there an easy way to do this, or do I have to figure out a function for myself?
    :
    : Cheers in advance
    :
    The only way of doing this is checking if the name or IP already exists before adding the item. The IndexOf() method will come in handy for this.
  • : : Hi all
    : :
    : : From my testings, "stringlist.Duplicates := dupIgnore/dupError/dupAccept" doesn't appear to help with my problem.
    : :
    : : Quick rundown of application:
    : : Two edit fields (Name/IPAddress), 1 button (Add), 1 listbox
    : : When you press the button, it first adds the edName.Text part to the listbox, and then it adds both edName.Text and edIPAddress.Text to a stringlist as a Name=Value pair, which then gets saved to a file.
    : :
    : : What I want to do is to prevent doubling up of either the Name or the IPAddress. dupIgnore/Error only works if both Name and IPAddress are the same the second time around, due to it looking at the entire string
    : : (Name=IPAddress instead of Name or IPAddress by themselves).
    : :
    : : Is there an easy way to do this, or do I have to figure out a function for myself?
    : :
    : : Cheers in advance
    : :
    : The only way of doing this is checking if the name or IP already exists before adding the item. The IndexOf() method will come in handy for this.
    :

    Can IndexOf() look parts of a string? I've been trying to use it, but it only seems to look at complete strings. This is a problem, since the stringlists get stored looking like:
    AWS-2500=192.168.0.42
    Diesel Tank=192.168.0.21
    Workshop=192.168.0.41
    IndexOf() returns -1 if I look for:
    sl.IndexOf('WorkShop') / sl.IndexOf(edName.Text)
    or:
    sl.IndexOf('192.168.0.41) / sl.IndexOf(edIP.Text)
    but it returns the proper index when it's:
    sl.IndexOf('WorkShop=192.168.0.41') / sl.IndexOf(edName.Text+'='+edIP.Text)

    I know I can write a function that delimits the strings by the '=' sign, and then do the checking that way, but I'd like it if I could do the task in only a few of lines of code, if possible.
  • : : : Hi all
    : : :
    : : : From my testings, "stringlist.Duplicates := dupIgnore/dupError/dupAccept" doesn't appear to help with my problem.
    : : :
    : : : Quick rundown of application:
    : : : Two edit fields (Name/IPAddress), 1 button (Add), 1 listbox
    : : : When you press the button, it first adds the edName.Text part to the listbox, and then it adds both edName.Text and edIPAddress.Text to a stringlist as a Name=Value pair, which then gets saved to a file.
    : : :
    : : : What I want to do is to prevent doubling up of either the Name or the IPAddress. dupIgnore/Error only works if both Name and IPAddress are the same the second time around, due to it looking at the entire string
    : : : (Name=IPAddress instead of Name or IPAddress by themselves).
    : : :
    : : : Is there an easy way to do this, or do I have to figure out a function for myself?
    : : :
    : : : Cheers in advance
    : : :
    : : The only way of doing this is checking if the name or IP already exists before adding the item. The IndexOf() method will come in handy for this.
    : :
    :
    : Can IndexOf() look parts of a string? I've been trying to use it, but it only seems to look at complete strings. This is a problem, since the stringlists get stored looking like:
    : AWS-2500=192.168.0.42
    : Diesel Tank=192.168.0.21
    : Workshop=192.168.0.41
    : IndexOf() returns -1 if I look for:
    : sl.IndexOf('WorkShop') / sl.IndexOf(edName.Text)
    : or:
    : sl.IndexOf('192.168.0.41) / sl.IndexOf(edIP.Text)
    : but it returns the proper index when it's:
    : sl.IndexOf('WorkShop=192.168.0.41') / sl.IndexOf(edName.Text+'='+edIP.Text)
    :
    : I know I can write a function that delimits the strings by the '=' sign, and then do the checking that way, but I'd like it if I could do the task in only a few of lines of code, if possible.
    :
    You can use IndexOfName() and IndexOfValue() (check the latter with the helpfiles). Otherwise you need to loop through it and check using the Pos() function:
    [code]
    for i := 0 to sl.Count-1 do
    if Pos(Name, sl[i])+Pos(IP, sl[i]) = 0 then
    sl.Add(Name+'='+IP);
    [/code]
  • *snip*
    : You can use IndexOfName() and IndexOfValue() (check the latter with the helpfiles). Otherwise you need to loop through it and check using the Pos() function:
    : [code]
    : for i := 0 to sl.Count-1 do
    : if Pos(Name, sl[i])+Pos(IP, sl[i]) = 0 then
    : sl.Add(Name+'='+IP);
    : [/code]
    :

    Thanks for your help :)

    IndexOfValue() only appears to exist for the .Net framework unfortunately.
    I had a bit of trouble with getting your code working just right, so for the time being I've setup a quick&dirty method of creating a second stringlist, which gets populated by the first stringlist's delmited values, and then IndexOf works perfectly.

    Good enough for small stringlists, but I'll revise it utilising the Pos() function to avoid huge overheads for really large stringlists.

    Cheers!
  • : *snip*
    : : You can use IndexOfName() and IndexOfValue() (check the latter with the helpfiles). Otherwise you need to loop through it and check using the Pos() function:
    : : [code]
    : : for i := 0 to sl.Count-1 do
    : : if Pos(Name, sl[i])+Pos(IP, sl[i]) = 0 then
    : : sl.Add(Name+'='+IP);
    : : [/code]
    : :
    :
    : Thanks for your help :)
    :
    : IndexOfValue() only appears to exist for the .Net framework unfortunately.
    : I had a bit of trouble with getting your code working just right, so for the time being I've setup a quick&dirty method of creating a second stringlist, which gets populated by the first stringlist's delmited values, and then IndexOf works perfectly.
    :
    : Good enough for small stringlists, but I'll revise it utilising the Pos() function to avoid huge overheads for really large stringlists.
    :
    : Cheers!
    :

    Hello,

    You might try this:
    [code]
    sl.Sorted:=true;
    sl.CaseSensitive:=true;
    sl.Duplicates:=DupIgnore;
    sl.Add(Name+'='+IP);
    [/code]

    Always works for me.

    James

Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories