Delphi and Kylix

Moderators: pritaeas
Number of threads: 7244
Number of posts: 19051

This Forum Only
Post New Thread
Single Post View       Linear View       Threaded View      f

Report
Preventing duplicate items in a stringlist Posted by LordBug on 28 Feb 2006 at 9:05 PM
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
Report
Re: Preventing duplicate items in a stringlist Posted by zibadian on 28 Feb 2006 at 10:02 PM
: 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.
Report
Re: Preventing duplicate items in a stringlist Posted by LordBug on 28 Feb 2006 at 11:24 PM
: : 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.
Report
Re: Preventing duplicate items in a stringlist Posted by zibadian on 1 Mar 2006 at 12:10 AM
: : : 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:
  for i := 0 to sl.Count-1 do
    if Pos(Name, sl[i])+Pos(IP, sl[i]) = 0 then
      sl.Add(Name+'='+IP);

Report
Re: Preventing duplicate items in a stringlist Posted by LordBug on 1 Mar 2006 at 11:13 PM
*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:
:
:   for i := 0 to sl.Count-1 do
:     if Pos(Name, sl[i])+Pos(IP, sl[i]) = 0 then
:       sl.Add(Name+'='+IP);
: 

:

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!
Report
Re: Preventing duplicate items in a stringlist Posted by jamesb800 on 2 Mar 2006 at 1:21 PM
: *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:
: :
: :   for i := 0 to sl.Count-1 do
: :     if Pos(Name, sl[i])+Pos(IP, sl[i]) = 0 then
: :       sl.Add(Name+'='+IP);
: : 

: :
:
: 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:
  sl.Sorted:=true;
  sl.CaseSensitive:=true;
  sl.Duplicates:=DupIgnore;
  sl.Add(Name+'='+IP);


Always works for me.

James




 

Recent Jobs

Official Programmer's Heaven Blogs
Web Hosting | Browser and Social Games | Gadgets

Popular resources on Programmersheaven.com
Assembly | Basic | C | C# | C++ | Delphi | Flash | Java | JavaScript | Pascal | Perl | PHP | Python | Ruby | Visual Basic
© Copyright 2011 Programmersheaven.com - All rights reserved.
Reproduction in whole or in part, in any form or medium without express written permission is prohibited.
Violators of this policy may be subject to legal action. Please read our Terms Of Use and Privacy Statement for more information.
Operated by CommunityHeaven, a BootstrapLabs company.