Recent

Author Topic: Formatting a TXT file with delimiters  (Read 2365 times)

jufau

  • New Member
  • *
  • Posts: 42
Formatting a TXT file with delimiters
« on: September 03, 2015, 03:26:24 pm »
Hi everyone!
I have some Text files to be imported to my database. These files simply don't have a field delimiter. This is pretty much the actual situation of my files:
79324817350003235658981449032
The description for this file is as follow:
characters
1 to 10 represents the telephone number
11 to 24 represents the company tax ID
25 to 30 represents a zip code
... it might have more fields depending on each file. I have files with 10 more fields than this one.

I am planning to use the "insert" pascal function to add the desired character in each line of the txt file. e.g:
var
s:string;
begin
    s:=';';
    Insert(myFileCurentLIne, s, 11);
    ...
end;

That should work for the  first set of characters. But what about the other sets (11-24, 25-30, etc)?
I was wondering if this is the case to use Records, and if so, how could a fill my record var with multiple values according to the amount of needed delimiter for each file? Let's say I need to add ";" 5 times on the position 11, 25, 31, 39, and 44 on each line of my txt file.
How to solve this problem?

Thank you all,
J

derek.john.evans

  • Guest
Re: Formatting a TXT file with delimiters
« Reply #1 on: September 03, 2015, 03:43:57 pm »

**EDIT** improved.
Code: [Select]
procedure InsertDelimiters(var AString: String; const APositions: array of Integer; const ADelimiter: Char = ';');
var
  LIndex: Integer;
begin
  for LIndex := 0 to High(APositions) do begin
    Insert(ADelimiter, AString, APositions[LIndex] + LIndex);
  end;
end;   

Code: [Select]
  S := '79324817350003235658981449032';
  InsertDelimiters(S, [11, 25]);   
« Last Edit: September 03, 2015, 03:48:01 pm by derek.john.evans »

taazz

  • Hero Member
  • *****
  • Posts: 5368
Re: Formatting a TXT file with delimiters
« Reply #2 on: September 03, 2015, 03:54:38 pm »
why use insert? Are you going to save the new line and reload it with some sort of csv component suite? Wouldn't that double your processing time? Why not simple use copy to get the value and assign it to the appropriate filed in your application and avoid wasting disk and cpu?
Good judgement is the result of experience … Experience is the result of bad judgement.

OS : Windows 7 64 bit
Laz: Lazarus 1.4.4 FPC 2.6.4 i386-win32-win32/win64

Jurassic Pork

  • Hero Member
  • *****
  • Posts: 1228
Re: Formatting a TXT file with delimiters
« Reply #3 on: September 03, 2015, 06:05:59 pm »
hello,
to extract the numbers, you can also use regular expression (uses regexpr)  like this for example :
Code: [Select]
procedure TForm1.Button1Click(Sender: TObject);
const cString = '79324817350003235658981449032';
var regex : TRegexpr;
    iMatch: Integer;
begin
regex := Tregexpr.Create();
regex.Expression := '^.*([0-9]{10,10})([0-9]{14,14})([0-9]{5,5}).*';
regex.Exec(cString);
if (regex.SubExprMatchCount =3 )then
ShowMessage('phone number : ' + regex.Match[1] + #13#10 +
           'ID : ' + regex.Match[2] + #13#10 +
           'Zip Code : ' + regex.Match[3]);
regex.Free;
end;

something strange in your message  you say 25 to 30 represents a zip code  (6 numbers) and you last part 49032 has only 5 numbers   :o

Friendly J.P
« Last Edit: September 03, 2015, 06:14:34 pm by Jurassic Pork »
Jurassic computer : Sinclair ZX81 - Zilog Z80A à 3,25 MHz - RAM 1 Ko - ROM 8 Ko

 

TinyPortal © 2005-2018