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

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
Alpha_AIAsked:
Who is Participating?
 
gangsterbossConnect With a Mentor Commented:
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
 
Alpha_AIAuthor Commented:
or maybe the set doesn't change all the time but grows and ungrows.
maximum of two words per set always.
0
 
alsantosCommented:
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
Cloud Class® Course: Python 3 Fundamentals

This course will teach participants about installing and configuring Python, syntax, importing, statements, types, strings, booleans, files, lists, tuples, comprehensions, functions, and classes.

 
Alpha_AIAuthor Commented:
hi
0
 
emu10k1Commented:
Hi :)
0
 
alsantosCommented:
Hi
0
 
Alpha_AIAuthor Commented:
hmmm
0
 
kretzschmarCommented:
what do you mean with set?

btw. hi :-))
0
 
swift99Commented:
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
 
Alpha_AIAuthor Commented:
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
 
swift99Commented:
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
 
Wim ten BrinkSelf-employed developerCommented:
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
 
Alpha_AIAuthor Commented:
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
 
Alpha_AIAuthor Commented:
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.