• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 579
  • Last Modified:

RichEdit - Insert Text "before" Column if does not Exist.

(If you can do something like this, I will open a new question and post the link in here)

Similuar to this line (I would think)
if (csvList.Filter(6, foNEQ, 'Bad information') > 0) then

I need to look for in the "same file" a certain area and see if it contains a
http://
And if it does not. Then I need to enter:
"something"

Example:

"1","345456","some product","some information here","45.65","more information","http://someurl"
"1","788589","some product","some information here","56.65","more information","Category One",'Good information","http://someurl"

In the above Example:
1st line Does Not contain the word:   "Category One"
2nd line Does contain the word:        "Category One"

What I need to do is look at each line and see if at a "Certain Point" the word is found, and if it is not
Then I will need to [Add] in the word that is needed.
In the case of the Example above.
I would search the 2-lines for the word "Category One" at the [6] location and if I find "http://
Then I will need to insert "Before" "http://" with the word "Category One"

I hope that is a good explanation?
Wayne
0
Wayne Barron
Asked:
Wayne Barron
  • 7
  • 7
1 Solution
 
Russell LibbySoftware Engineer, Advisory Commented:

Example usage:

   // Assuming richedit2 contains the data to be update
   UpdateDeptData(RichEdit2.Lines, '[No Department Available]');


// Procedure to update the TStrings(TStringList) data so that the department field is inserted
// if missing from the row
procedure UpdateDeptData(Data: TStrings; DeptData: String);
var  listData:      TStringList;
     listFields:    TStringList;
     dwIndex:       Integer;
     dwFields:      Integer;
     szRow:         String;
begin

  // Lock the string list
  Data.BeginUpdate;
  try
     // Create string list for field processing
     listFields:=TStringList.Create;
     try
        // Walk the list items
        for dwIndex:=0 to Pred(Data.Count) do
        begin
           // Clear field processing string
           szRow:=EmptyStr;
           // Parse list into individual fields
           listFields.CommaText:=Data[dwIndex];
           // Check field count and value of the 29th field
           if (listFields.Count = 31) and (Pos('http://', LowerCase(listFields[29])) = 1) then
           begin
              // Insert department field
              listFields.Insert(29, DeptData);
              // Recreate the quoted comma sep string
              for dwFields:=0 to Pred(listFields.Count) do szRow:=szRow+AnsiQuotedStr(listFields[dwFields], '"')+',';
              // Remove trailing comma
              Delete(szRow, Length(szRow), 1);
              // Update the data
              Data[dwIndex]:=szRow;
           end;
        end;
     finally
        // Free fields list
        listFields.Free;
     end;
  finally
     // Unlock the string list
     Data.EndUpdate;
  end;

end;
0
 
Wayne BarronAuthor Commented:
Thank you very much Russell for your help.
You are one of the best here @ EE.

Wayne
0
 
Wayne BarronAuthor Commented:
Hello Russell.
Hope that you had a great weekend?

Got a question for you please Sir?
It is regarding the code here.

In the following area's

(Pos('http://', LowerCase(listFields[29])) = 1) then

&

listFields.Insert(29, DeptData);

I am needing to change the line #'s and Text at any given time.
So without having to edit the actual Source-Code. I was trying to add a TEdit to the properties.
But :

Where the:    'http://',  <-- I added:   ''+Edit4.text+'',   <-& tried-> (Edit4.Text) Neither work, recieve errors.
Where the:   (listfields[29]))  <-- I added:  (Edit3.Text) (I think this one works??)
same with this:   listFields.Insert(29, DeptData);<-- I added: (Edit3.Text) <-- This does not work.

Any idea's on how to make these area's work with TEdit's?
Hopefully it does not require redoing the code except maybe a little?

Thanks Russell;
Wayne
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
Russell LibbySoftware Engineer, Advisory Commented:
Wayne,

Sorry, I am not following you at all. Based on what you clipped above, I can only guess that you are attempting something along the lines of:

---
        if (listFields.Count = 31) and (Pos(Edit4.Text, Edit3.Text) = 1) then
           begin
              // Insert department field
              listFields.Insert(29, Edit3.Text);
---

But if you replace listfields[29], what are you then comparing when you process the current line??? You also indicated errors, but didn't mention *what* the errors were. (makes it very difficult to help). I can only guess that the problem may be due to scoping. The procedure I gave you:

procedure UpdateDeptData(Data: TStrings; DeptData: String);

is scoped at the unit level, and you are attemping to use edit controls directly, which are scoped at the form level, and should be qualified as FormName.Edit3, FormName.Edit4, etc. Either that or you can change the procedure to be that of the form.
eg:

procedure TFormName.UpdateDeptData(Data: TStrings; DeptData: String);
begin
 ...
end;

which would let you use the edit fields directly.

--

But back to the original problem, what is it exactly that you are trying to accomplish? I know you are trying to insert fields, but HOW are you determining when to insert, and where to insert?

Regards,
Russell
0
 
Wayne BarronAuthor Commented:
OK, Sorry, I should have left it the way I originally wrote it up.

The area's that are hardcoded such as:
====================================================
if (listFields.Count = 31) and (Pos('http://', LowerCase(listFields[29])) = 1) then
           begin
              // Insert department field
              listFields.Insert(29, DeptData);
====================================================
This is what I need to do:
====================================================
if (listFields.Count = (Form1.Edit5.Text)) and (Pos((Form1.Edit4.Text), LowerCase(listFields[(Form1.Edit3.Text)])) = 1) then
           begin
              // Insert department field
              listFields.Insert((Form1.Edit3.Text), DeptData);
====================================================

Due to the different area's that I will be having to edit in the Fields.
I will need to be able to have the option to choose the correct line.
And working with different [Datafeeds] from different companies.
I will need to be able to choose the Amount of Line(s) in the Datafeed without
Having to always bring open Delphi to edit the #'s in the code.
-----------------
The Error's that I was receiving are:

if (listFields.Count = (Form1.Edit5.Text)) --> Incompatible types: 'String' and 'Integer'
(Pos((Form1.Edit4.Text),   -->This one works, without an Error. Which is good.
LowerCase(listFields[(Form1.Edit3.Text)])) --> Error: Incompatible types: 'Integer' and 'TCaption'
listFields.Insert((Form1.Edit3.Text), DeptData); --> Error: Incompatible types: 'Integer' and 'TCaption'
--------
Sorry that I did not add this information in and leave it, instead of editing it.
I had already tried the   Form1.Edit3.Text
And that is the reason why I decided to ask you, if you can assist on it.
--------
Thanks for your assistance.
Wayne
0
 
Russell LibbySoftware Engineer, Advisory Commented:

Wayne,
The errors are the result of using string data where an integer is required. You need to convert the data, and can use StrToInt (which will raise an exception on conversion error), or StrToIntDef, which allows you to seed a default value on conversion failure.

Eg:

if (listFields.Count = StrToInt(Form1.Edit5.Text)) and (Pos((Form1.Edit4.Text), LowerCase(listFields[StrToInt(Form1.Edit3.Text)])) = 1) then
           begin
              // Insert department field
              listFields.Insert(StrToInt(Form1.Edit3.Text), DeptData);

Russell




0
 
Wayne BarronAuthor Commented:
Thank you Russell.
I forgot about the "StrToInt"
I have not used it in a few years, as a lot of my recent project of been based around the TEmbeddedWB.
So, with the actual Text part of Delphi projects, I have forgotten about.
BUT. I have a Repository Database Project that I made up back last year, that I use to add in information
So this is one thing that I will be adding into it.

Thanks for your helpful advice once again.
Wayne
0
 
Russell LibbySoftware Engineer, Advisory Commented:
No problem

Russell
0
 
Wayne BarronAuthor Commented:
Hello Russell;

On this line here:

 if (listFields.Count = 31) and (Pos('http://', LowerCase(listFields[29])) = 1) then

Is there anyway possible to find the [Field] without having to look for any paticular text?
In this line above, we are looking for:    http://

The reason why I am asking, is that I have to add in some text (Half of a URL    /folder/file/image.gif)
Into the Datafeed. THe problem is, is that the [Field] that is after is Numbers, and they vary, they are never the same.

I tried removing the   http://  and leaving it null:   '',  But that did not work, of which I did not think that it would.

Any idea's on this one?
I can open a new question if you have a solution?

Take Care and have a great weekend.
I have had a decent one so far.
I am a Big [KISS] fan, and I just saw & Recorded them from the VH1 Rock Honors tonight,
That was pretty cool. I missed it when it first aired on Wednesday, so I caught it tonight at 12:00 midnight.

Take Care
Wayne
0
 
Russell LibbySoftware Engineer, Advisory Commented:
Wayne,

Glad to hear you had a good weekend. As to the question, are you just wanting to modify an existsing field? The code has all the fields broken out in the string list "listFields". So if you need to access the field directly, you can.

eg:
if (listFields.Count = 31) then
begin
  value:=listFields[FieldIndex];
  ...

And set any new value back as needed:

listFields[FieldIndex]:=UpdateValue;

If this is not what your after, more (detailed) info would be appreciated ;-)

Regards,
Russell

0
 
Wayne BarronAuthor Commented:
Thank you Russell.
I had to think for a minute :) Had already forgotten about asking you this.

Basically what I was asking was:
If the following applies:

"Something","Needs","To","Be","added","between","$6,785","$9,687"
// The line above is missing the ","+"," In reality, there is about 20-lines that need to have the ","+"," Added
// So I cannot choose the [Value] that is there ","$6,785"," (or) ","9,687"," As they are never the same.

// How it suppose to look
"Something","Needs","To","Be","added","between","$6,785","+","$9,687"

So in this case, this would be:
[8] Fields
I would need to look in the
[7] Field, and see if the item exist, and if it does not. Then Add it in between the other [6] & [8] Field(s)

I can understand this, I hope that I wrote so that you can.
// I would think that it would be written out something like this? But do not think that I did it right?
if (listFields.Count = StrToInt(Form1.Edit5.Text)) then
begin
value:=listFields[FieldIndex];
              listFields.Insert(StrToInt(Form1.Edit3.Text), DeptData);
listFields[FieldIndex]:=UpdateValue;

Wayne
0
 
Russell LibbySoftware Engineer, Advisory Commented:

Wayne, only the insert would be required.


 if (listFields.Count > 7) and (listFields[7] <> '+') then
 begin
    listFields.Insert(7, '+');
    ...

Russell


0
 
Wayne BarronAuthor Commented:
OK, I will take a look at it after while.
Go my feet deep in another project right now.

Thank you Russell.
If anyone has not told you that you are one of the best lately, then:
You are one of the Best.

Take Care
Wayne
0
 
Russell LibbySoftware Engineer, Advisory Commented:
lol, thanks... very nice to hear.

Back to some C conversion for me, let me know if your run into trouble,
Russell
0

Featured Post

How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

  • 7
  • 7
Tackle projects and never again get stuck behind a technical roadblock.
Join Now