Solved

How do i test to see if a word is inside a set words

Posted on 2003-10-25
16
228 Views
Last Modified: 2010-04-05
Hi,
How do I test to see if a word is in a set of words that dynamically change all the time.
Any clues?
I dont know much on sets.

Alpha
0
Comment
Question by:Alpha_AI
  • 6
  • 2
  • 2
  • +4
16 Comments
 

Author Comment

by:Alpha_AI
ID: 9621373
or maybe the set doesn't change all the time but grows and ungrows.
maximum of two words per set always.
0
 
LVL 2

Expert Comment

by:alsantos
ID: 9621800
If I undertood your question, you can try this code:

type
  TColor = (Green, Blue, Black);
  TColors = set of TColor;

procedure TForm1.Button1Click(Sender: TObject);
var
  Color: TColors;
begin
  Color:=[Green];
  Color:=Color + [Black];
  if not (Blue in Color) then showmessage('Blue isnt in Color');
  if (Black in Color) then showmessage('Black is in Color');
end;

alsantos
0
 

Author Comment

by:Alpha_AI
ID: 9621824
hi
0
 

Expert Comment

by:emu10k1
ID: 9621834
Hi :)
0
 
LVL 2

Expert Comment

by:alsantos
ID: 9621840
Hi
0
 

Author Comment

by:Alpha_AI
ID: 9621848
hmmm
0
 
LVL 27

Expert Comment

by:kretzschmar
ID: 9621998
what do you mean with set?

btw. hi :-))
0
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 6

Expert Comment

by:swift99
ID: 9622427
Here's a couple more approaches ...

1. Throw all of you "set" into a string (called "wordSet" in this example) separated by commas or psaces

wordInSet := pos (word, wordSet) > 1;

2. Throw all of your words into a TStringList, also called "wordSet" in this example

wordInSet := wordInSet.IndexOf (word)>0

Since your "wordSet" will always be small,there is no need to get fancy and use hash tables or anything like that.

0
 

Author Comment

by:Alpha_AI
ID: 9624654
well from what i know is that sets cannot deal with data that is dynamic from example of alsantos, the word has to already exist in the set to manipulate with it.

yeah i can see where you are coming from swift99, i have thought of doing it the same way before and still would but i wanted to use sets to see what they could offer. I guess it can't be done cos maybe it can only deal with words that originally are in the set .

if there is isn't a way of doing it using sets, then i might have to just uses Tstrings or TStringList. I would then have to delete the question because no one has come up a solution for dynamic data in sets.
Thanks for ur time
alpha



0
 
LVL 6

Expert Comment

by:swift99
ID: 9625007
A set compiles to an array of 256 bits (not bytes).  It can be thought of as a "packed array [0..255] of boolean" for pragmatic reasons.  Each bit in the set maps at compile time to an ordinal.

Strings can not compile to sets.  Only if you can map the strings to byte values, and then use the byte values against the set.  But then you need to guarantee that you will only ever have 256 distinct values.

In short, the pascal "set" mechanism is not suited to this task.  You would have to use the string or string list mechanism to map to the byte value before you could use the set, so why add the overhead?

I'm biased   :o)  but I think that the information that says why it is not possible has to be worth something.  Call it your way, I'm not greedy.
0
 
LVL 17

Expert Comment

by:Wim ten Brink
ID: 9627620
A set is limited to ordinal types with a maximum of 256 elements. Let's take this set: color = set of (red, orange, green);
Now, to empty it, use: Color := [];
To add an element use: Include(Color, Red);
To remove an element use: Exclude(Color, Green);
To see if a color is in the set use: if (Orange in Color) then ...
There are some arithmetical methods to combine sets but I'll skip those. Never use them anyway...

Now, if you meant a set of string then I have to disappoint you. Not allowed. Use a TStringList instead. Something like:
var
  Color:TStringList;
begin
  // Create thingie...
  Color := TStringList.Create;
  // Sort it and make values unique.
  Color.Sorted := True;
  Color.Duplicates := dupIgnore;
  // Add some data.
  Color.Add('Red');
  Color.Add('Orange');
  Color.Add('Green');
  // See if a color is in the list.
  if Color.IndexOf('Red')>=0 then // We have Red.
  // Remove a color
  if Color.IndexOf('Red')>=0 then Color.Delete(Color.IndexOf('Red'));

And there are more uses for string lists...
0
 

Author Comment

by:Alpha_AI
ID: 9632094
Hi, is it fair that i delete the question to regain the points?
cos i need the points for another question and no one has come up with a way of handling dynamic data inside sets.
Stupid me put all the points on a question which is not receiving the correct answer.

However since I have to talk to the moderator to get this deleted. To save me doing that
i would like to ask another question here right now and the person who knows the answer to this will get the points. Is this ok with you?

Here is the question:

I have created my own little copy of the mail demo by indy. I can send messages ok but when i receive them, the body is empty. What is the problem? The demo also has the same problem when used with delphi7.

procedure TForm1.Button5Click(Sender: TObject);
begin
CheckMailExecute;
end;

procedure TForm1.CheckMailExecute;
begin
//   Showbusy(true);
//   ShowStatus('Connecting....');
   if POP.Connected then
      begin
         POP.Disconnect;
      end;
   POP.Host := Form6.LabeledEdit1.Text;
   POP.Port := 110;
   POP.Username := Form6.LabeledEdit2.Text;
   POP.Password := Form6.LabeledEdit3.Text;
   POP.Connect(5999);
//   ToggleStatus(True);
   FMsgCount := POP.CheckMessages;
   FMailBoxSize := POP.RetrieveMailBoxSize div 1024;
//   ShowFileStatus;
   if FMsgCount > 0 then
      begin
//         ShowFileStatus;
         RetrievePOPHeaders(FMsgCount);
      end
   else
      begin
//         ShowStatus('No messages on server');
      end;
//   Showbusy(false);
end;

procedure TForm1.RetrievePOPHeaders(inMsgCount: Integer);
var
   stTemp: string;
   intIndex: integer;
   itm: TListItem;
begin
//   stTemp := Statusbar1.Panels[1].text;
   lvHeaders.Items.Clear;
   for intIndex := 1 to inMsgCount do
      begin
         Application.ProcessMessages;
         Msg.Clear;
         Msg.ContentType := 'text/plain';
         POP.RetrieveHeader(intIndex, Msg);
         // Add info to ListView
         itm := lvHeaders.Items.Add;
         itm.ImageIndex := 5;
         itm.Caption := Msg.Subject;
         mSubject := Msg.Subject;
         itm.SubItems.Add(Msg.From.Text);
         mFrom := Msg.From.Text;
         itm.SubItems.Add(DateToStr(Msg.Date));
         itm.SubItems.Add(IntToStr(POP.RetrieveMsgSize(intIndex)));
         itm.SubItems.Add('n/a');
//         itm.SubItems.Add(POP.RetrieveUIDL(intIndex));
      end;
//   ShowStatus(stTemp);
end;

procedure TForm1.RetrieveExecute(Sender: TObject);
var
   stTemp: string;
   intIndex: Integer;
   li: TListItem;
begin
//   stTemp := Statusbar1.Panels[1].text;
//   if lvHeaders.Selected = nil then
//      begin
//         Exit;
//      end;
//   Showbusy(true);
   Msg.Clear;
   Memo4.Clear;
//   lvMessageParts.Items.Clear;
//   From.Caption := '';
//   Cc.Caption := '';
//   Subject.Caption := '';
//   Date.Caption := '';
//   Receipt.Caption := '';
//   Organization.Caption := '';
//   Priority.Caption := '';
//   pnlAttachments.visible := false;

   //get message and put into MSG
   Form1.Caption := 'Retrieving message "' + lvHeaders.Selected.SubItems.Strings[intindex] + '"';
   POP.Retrieve(lvHeaders.Selected.Index + 1, Msg);
//   statusbar1.Panels[0].text := lvHeaders.Selected.SubItems.Strings[intindex];

   //Setup fields on screen from MSG
//   From.Caption := Msg.From.Text;
//   Recipients.Caption := Msg.Recipients.EmailAddresses;
//   Cc.Caption := Msg.CCList.EMailAddresses;
//   Subject.Caption := Msg.Subject;
//   Date.Caption := FormatDateTime('dd mmm yyyy hh:mm:ss', Msg.Date);
//   Receipt.Caption := Msg.ReceiptRecipient.Text;
//   Organization.Caption := Msg.Organization;
//   Priority.Caption := IntToStr(Ord(Msg.Priority) + 1);

   //Setup attachments list
//   ShowStatus('Decoding attachments (' + IntToStr(Msg.MessageParts.Count) + ')');
   for intIndex := 0 to Pred(Msg.MessageParts.Count) do
      begin
         if (Msg.MessageParts.Items[intIndex] is TIdAttachment) then
            begin //general attachment
       //        pnlAttachments.visible := true;
       //        li := lvMessageParts.Items.Add;
       //        li.ImageIndex := 8;
       //        li.Caption := TIdAttachment(Msg.MessageParts.Items[intIndex]).Filename;
       //        li.SubItems.Add(TIdAttachment(Msg.MessageParts.Items[intIndex]).ContentType);
            end
         else
            begin //body text
               if Msg.MessageParts.Items[intIndex] is TIdText then
                  begin
                     Memo3.Lines.Clear;
                     Memo3.Lines.AddStrings(TIdText(Msg.MessageParts.Items[intindex]).Body);
                  end
            end;
      end;
//   ShowStatus(stTemp);
//   Showbusy(false);
   end;
 



0
 

Author Comment

by:Alpha_AI
ID: 9632101
And the funny thing is that if the email is an html document, the text of the html code will show up which is real weird but the other document bodies are a no show.
0
 
LVL 2

Accepted Solution

by:
gangsterboss earned 85 total points
ID: 9647151
this will highlight all the word that are the same as the selected word

procedure TForm1.ColorizedWordall(oRichedit : TRichedit; sWord : String; oColor : TColor);
var
iFoundAt,
iSelStart,
iSelLength : Integer;
begin
iSelStart := oRichedit.SelStart;
iSelLength := oRichedit.SelLength;
try
  iFoundAt := -1;
  repeat
    iFoundAt := oRichedit.FindText(sWord,iFoundAt+1,length(oRichedit.Text),[]);
    if iFoundAt > -1 then
    begin
      oRichedit.SelStart := iFoundAt;
      oRichedit.SelLength := length(sWord);
      oRichedit.SelAttributes.color:= clgreen;// this can be any color
    end;
  until iFoundAt = -1;
finally
  oRichedit.SelStart := iSelStart;
  oRichedit.SelLength := iSelLength;
end;

end;
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

In this tutorial I will show you how to use the Windows Speech API in Delphi. I will only cover basic functions such as text to speech and controlling the speed of the speech. SAPI Installation First you need to install the SAPI type library, th…
In my programming career I have only very rarely run into situations where operator overloading would be of any use in my work.  Normally those situations involved math with either overly large numbers (hundreds of thousands of digits or accuracy re…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

759 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now