Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Help needed from a coding guru!! 3

Status
Not open for further replies.

delphiman

Programmer
Dec 13, 2001
422
ZA


On the one hand I have a .TXT file which is a copy (snapshot) of a bank account statement.
On the other hand I have a DataBase with fields DATE NAME SNO Amount .

Amonst the lines SOME of them (the only one's I am interested in) will have the following
data which I need to capture into the DataBase:-

DATE SOMENAME SOME_NUM DOLLARS.

The lines in the .TXT file appear as follows:-

33382190495 DATE DEPOSIT - ER SOMENAME SOME_NUM DOLLARS
33382190495 DATE DEPOSIT - ER ANOTHERNAME SOME_NUM DOLLARS

Where the constants in the only lines I am interested in are
33382190495 [Some space] A DATE [Some space] DEPOSIT [Some space] - [Some space] ER [Some space]
as above.

After that there would be [SOMENAME] [Some space] [8 Characters] [Some space] [Money]

The ONLY variables would be
DATE (Varying from line-to-line)
SOMENAME (Which would be someone's last name and vary in length from line-to-line.)
DOLLARS (Which could be any amount.)

What code can I use to scan this data so that I can capture the data I want from these
lines only into the above DataBase fields?

Anybody...? Puhlease??

 
Hi Delphiman,

Well if every line starts with 33382190495 you could use that to find all the lines by checking line by line (try playing around with SubString procedure). You could put all the lines in an array. Then you could just read them out by deleting data youve already gotten and then use the TrimLeft function to remove all the spaces on the left side of the string, and then you could use a simple loop to count the number of characters before the next space is encountered (this could be problematic when the name includes spaces, maybe if there is always at least two spaces between the next piece of data you could do it like that), and then just yank the info out. Then repeat until there are no characters left. By putting the lines in an array, you could use two loops (one inside the other).

Hope this helps,

[bobafett] BobbaFet [bobafett]

Everyone has a right to my opinion.
E-mail me at caswegkamp@hotmail.com
 
Have a look for some regular expression utilities....From memory you can specify some quite complex expressions and get at the 'variable' parts that you want.

I know HyperString supports simple regular expressions....I'm sure there would be some freeware/opensource units out there to do what you want.... worse case you could use Python for Delphi to pass the lines to Python and have it use its regular expression unit (re) to do it....
 
Thanks BobbaFet!

Would it be a imposition to ask you for an example? :) Some code perhaps. Anything!

Thanks BillDoorNZ!
....I'm sure there would be some freeware/opensource units out there to do what you want....

Any ideas as to where (URL?) one might look? :)

worse case you could use Python ...

Python??? My last contact with a Python didn't suggest that it might be friendly (or helpful) towards me (or anyone else) or that it might have any knowledge of Delphi. :) :)

for Delphi to pass the lines to Python and have it use its regular expression unit (re) to do it....

I'm afraid you have lost me there! Can you perhaps be kind enough to come back .... and tell me more? :)
 
Thanks BillDoorNZ!
Lot of stuff there (which I have downloaded) to wade through! :)
 
Hey Delphiman,

I'll give it a good shot, but since I'm at home there will probably be a bug or two in my code but here goes:

What I have done here is loading the bank.txt into a memo
and work from there (ow before anyone comments on my absurd use of procedures, it's because I like them better than functions).

Code:
MyArray: array of string; // notice that i dont give a fixed length as i dont know how many records itll have that correspond with my search, make this a global array so it can be accessed from anywhere, I suggest you put it under the private heading

procedure Form1.GetRequiredLines(Sender: TObject);
var i,j: integer; // Loop counters
TestStr: string; // Variable i use to find the right strings and put them into an array
begin
for i := 0 to (Memo1.Lines.Count - 1) do
      begin
      TestStr := Memo.Lines.String[i];
      if SubString(TestStr,1,11) = '33382190495' then 
            begin
            SetLength(MyArray,Length(MyArray)+1);
            MyArray[Length(MyArray) - 1] := TestStr;
            end;
      end;
GetDataFromArray;
end;

// So the procedure above will get the lines from the file.
// The procedure below will get the data from the lines.

procedure Form1.GetDataFromArray;
var i: integer;
StartStr: string; // Get string out of array to work with
Date, Surname, Amount: String; // The variables in the line
begin
// So as I said earlier you could use a loop within a loop to get all the data in a usable form, so let's start with that.
for i := 0 to Length(MyArray) do 
      begin
      StartStr := MyArray[i];
// apperantly you only use that number the line starts with to find the right lines so you can start by deleting that from your string
      Delete(StartStr,1,11);
      TrimLeft(StartStr); // Removes excess spaces on the left side of the string
      for j := 1 to Length(StartStr) do
      if StartStr[j] = ' ' then
            begin
            Date := SubString(StartStr,1,j); // Gets the date
            Delete(StartStr,1,j); // Deletes date from StartStr
            Break; // Stops this loop
            end;
      TrimLeft(StartStr);
// Now we come to the DEPOSIT area of the string we'll just delete that from the string
      for j := 1 to Length(StartStr) do
      if StartStr[j] = ' ' then
            begin
            Delete(StartStr,1,j); // Deletes DEPOSIT
            TrimLeft(StartStr);
            Break;
            end;
// Now we should encounter the dash '-', lets get rid of that too.
      if SubString(StartStr,1,1) = '-' then
      Delete(StartStr,1,1);
      trimLeft(StartStr);
// ER, apperantly not useful, delete it
      Delete(StartStr,1,2);
      TrimLeft(StartStr);
// The Surname area
      for j := 1 to Length(StartStr) do
      if StartStr[j] = ' ' then
            begin
            Surname := SubString(StartStr,1,j); 
            Delete(StartStr,1,j); 
            Break;
            end;
      TrimLeft(StartStr);
// Those 8 characters are up now
      Delete(StartStr,1,8); 
      TrimLeft(StartStr);
// The Amount area
      for j := 1 to Length(StartStr) do
      if StartStr[j] = ' ' then
            begin
            Amount := SubString(StartStr,1,j); 
            Delete(StartStr,1,j); 
            Break;
            end;
      end;
end;

This will cause you to have those 3 variables that you pointed out in your question in three variables. You could then put those into another array and pass that array to a function or procedure that will process them in the database. Another thing came to while typing this code was that maybe the total of a piece of information and following spaces always are the same. In that case you could just get the total as a substring and then use the Trim function to remove spaces from left and right side of the substring.

I hope this answers your question,

[bobafett] BobbaFet [bobafett]

Everyone has a right to my opinion.
E-mail me at caswegkamp@hotmail.com
 
Hi BobbaFet !
since I'm at home

That's very kind of you Mate! Thanks a million from downunder
 
Ow hehe, just noticed a mistake in the code, the J variable should be in the second procedure, not in the first. You probably saw that already though.

[bobafett] BobbaFet [bobafett]

Everyone has a right to my opinion.
E-mail me at caswegkamp@hotmail.com
 
Hi, you might find this procedure useful, I emulated the PICK BASIC, "FIELD" function.
Examples of use :-
Txt:= "a b c d" s:=field(Txt,' ',3); gives s = c
Txt:= "X,F2,VV3,4" s:=field(Txt,',',3); gives s = VV3
Txt:="a:b:c" s:=field(Txt,':',3) gives s = c
ie: get a field seperated by any characters.
This is a really useful function, I use it for decoding EDI.

the procedure :- just copy & paste it.

{======================================= Field FUNCTION }
{Returns the pth occurence in s delimited by ss }
function Tform1.Field(s,ss: string;p:integer):string;
var CurS: string; { Current segment }
CurP: integer; { Current Position (occurrence) }
begin
if p = 1 then begin {........ First occurence }
if pos(ss,s) = 0 then begin {... With No Delimeter in String }
result := s;
exit;
end;
result := copy(s,1,pos(ss,s)-1); {... With delimeters in string }
exit;
end;
if pos(ss,s) = 0 then begin {........ No Delimeter in String }
result := '';
exit;
end;
{.......................................... 2nd or more occurrence }
CurP := 0;
s := s+ss;
while Pos(ss,s) > 0 do begin
CurP := CurP+1;
CurS := copy(s,1,Pos(ss,s)-1);
s := copy(s,Pos(ss,s)+1, length(s));
if CurP = p then begin
result := CurS;
exit;
end;
end;
end;



Frank.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top