The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.
|
 |
|
Dev Shed Forums
> Programming Languages - More
> Delphi Programming
|
Extract desirable column from .txt file... how....
Discuss Extract desirable column from .txt file... how.... in the Delphi Programming forum on Dev Shed. Extract desirable column from .txt file... how.... Delphi Programming forum discussing Delphi related topics including Kylix, C++ Builder, and more. Delphi is a high-performance language, originally based on the PASCAL language.
|
|
 |
|
|
|
|
|

Dev Shed Forums Sponsor:
|
|
|

August 23rd, 2012, 05:57 AM
|
|
Registered User
|
|
Join Date: Jul 2012
Posts: 29
Time spent in forums: 4 h 36 m 52 sec
Reputation Power: 0
|
|
|
Extract desirable column from .txt file... how....
Dear my fellas..
I have a text file, containing student's number, students name, respectively..
Example:
343;john;doe
544;jessy;dolphin
113;michael;iron
.
.
My aim is, I want to extract 2nd column and send it to the combobox.
In example, I want to extract values 'john,jessy,michael,...' from .txt file .
How could I accomplish that?
Any idea, any example will be so helpful....
I am waiting.
|

August 23rd, 2012, 09:25 AM
|
|
Contributing User
|
|
Join Date: Jun 2008
Posts: 251
 
Time spent in forums: 2 Days 22 h 50 m 14 sec
Reputation Power: 5
|
|
You can accomplish this using the COPY function which returns a substring of a string and use POS and POSEX functions to define the start and end of your sub-string.
Code:
function ExtractName(InStr: String): String;
var
StartPos: Integer;
StrLength: Integer;
begin
StartPos := Pos(';',InStr)+1;
//Start just after ';'
StrLength := PosEx(';', InStr, StartPos) - StartPos;
//get pos of 2nd ';' subtract startpos to get length
result := Copy(InStr, StartPos, StrLength);
//Add StrUtils to your uses clause for PosEx function.
end;
Usage:
Code:
S := '343;john;doe';
ComboBox1.Items.Add(ExtractName(S));
//you'll need to do this in a loop assigning each string in turn.
|

August 26th, 2012, 04:14 PM
|
|
Registered User
|
|
Join Date: Jul 2012
Posts: 29
Time spent in forums: 4 h 36 m 52 sec
Reputation Power: 0
|
|
Changing a little bit it, I changed it to the format:
var
startpos:integer;
strlength:integer;
procedure ExtractName(InStr: String): String;
begin
startpos:=pos(';',Instr)+1;
strlength:=posex(';',instr,startpos)-startpos;
result:=copy(instr,startpos,strlength);
end;
then it gave me errors:
undeclared identifier: Instr
undeclared identifier: posex
undeclared identifier: result
I am not sure which data type they will have.
please help....
Quote: | Originally Posted by majlumbo You can accomplish this using the COPY function which returns a substring of a string and use POS and POSEX functions to define the start and end of your sub-string.
Code:
function ExtractName(InStr: String): String;
var
StartPos: Integer;
StrLength: Integer;
begin
StartPos := Pos(';',InStr)+1;
//Start just after ';'
StrLength := PosEx(';', InStr, StartPos) - StartPos;
//get pos of 2nd ';' subtract startpos to get length
result := Copy(InStr, StartPos, StrLength);
//Add StrUtils to your uses clause for PosEx function.
end;
Usage:
Code:
S := '343;john;doe';
ComboBox1.Items.Add(ExtractName(S));
//you'll need to do this in a loop assigning each string in turn. |
|

August 26th, 2012, 05:49 PM
|
|
Contributing User
|
|
Join Date: Jan 2006
Location: Carlsbad, CA
|
|
|
Why change what is correct?
1.
You changed from a function to a procedure therefore
undeclared identifier: result
2.
You moved
StartPos and StrLength variables outside the procedure.
Wrong approach
You should not declare local variables as global.
All variables should be declared with the least scope possible
to accomplish their task.
3.
undeclared identifier: posex
Did you include StrUtils in your uses clause as instructed?
4.
undeclared identifier: Instr
No idea. From what you actually posted, I can see no reason for that.
Is this an exact representation of your real code?
Alternatively, it might be related to your changing from function
to procedure without removing the return parameter from the declaration.
Clive
Last edited by clivew : August 26th, 2012 at 05:54 PM.
Reason: Added comment about declaration to item 4.
|

August 27th, 2012, 01:32 PM
|
|
Registered User
|
|
Join Date: Jul 2012
Posts: 29
Time spent in forums: 4 h 36 m 52 sec
Reputation Power: 0
|
|
|
Including strutils header to the program, and using function, works perfectly!..
Thank you.
|

August 27th, 2012, 01:46 PM
|
|
Contributing User
|
|
Join Date: Jun 2008
Posts: 251
 
Time spent in forums: 2 Days 22 h 50 m 14 sec
Reputation Power: 5
|
|
Quote: | Originally Posted by turkalpk Including strutils header to the program, and using function, works perfectly!..
Thank you.
One last question. How could I retrieve 1st column ? and 3rd column? |
Using the same functions (Copy, Pos and PosEx)
To get the 1st and 3rd columns
Code:
function FirstColumn(InStr: String): String;
begin
Result := copy(InStr, 1, pos(';', InStr)-1);
//return the string between the 1st character and
//just before the first ';'
end;
function ThirdColumn(InStr: String): String;
function PosN(LookFor, LookIn: String; nthPosition: Cardinal): Integer;
//This function will find the Nth occurrence of LookFor
//in LookIn.
//If it returns 0, then that Nth occurrence does not exist.
//You want to find the 2nd ';' in the string..
//notice that it is embedded within the ThirdColumn function
//It can stand as a separate function, but if the functionality
//isn't required elsewhere, keep it within ThirdColumn
var
I, CurPos: Integer;
begin
CurPos := 0;
For I := 1 to nthPosition do
begin
CursPos := PosEx(LookFor, LookIn, CurPos+1);
if CurPos := 0 then
Break;
end;
Result := CurPos;
end;
begin
Result := Copy(InStr, PosN(';', InStr, 2)+1);
//return the string after the 2nd ';'
end;
|

August 28th, 2012, 09:34 AM
|
|
Registered User
|
|
Join Date: Jul 2012
Posts: 29
Time spent in forums: 4 h 36 m 52 sec
Reputation Power: 0
|
|
|
thank you so much.
working perfectly.
|

September 4th, 2012, 03:36 PM
|
|
Registered User
|
|
Join Date: Jul 2012
Posts: 29
Time spent in forums: 4 h 36 m 52 sec
Reputation Power: 0
|
|
|
I could not be able to run function to get third column.
I could not be able to run function to get third column.
var i,curpos:integer;
function thirdcolumn(instr:string):string;
begin
function posn(LookFor,lookin:string;nthposition:cardinal):integer; begin
curpos:=0;
for i := 1 to nthposition do begin curpos:=posex(lookfor,lookin,curpos+1);
if curpos:=0 then break;
end;
result:=curpos;
end;
result:=copy(instr,posn(';',instr,2)+1);
end;
end.
Please help. I am waiting.
|

September 4th, 2012, 04:50 PM
|
|
Contributing User
|
|
Join Date: Jan 2006
Location: Carlsbad, CA
|
|
Quote: | result:=copy(instr,posn(';',instr,2)+1); |
Code:
result:=copy(instr,posn(';',instr,2)+1,maxint);
You forgot the last parameter for copy.
Using maxint, of course, assumes that the third column is the final column.
|

September 4th, 2012, 05:46 PM
|
|
Contributing User
|
|
Join Date: Jun 2008
Posts: 251
 
Time spent in forums: 2 Days 22 h 50 m 14 sec
Reputation Power: 5
|
|
Quote: | Originally Posted by clivew
Code:
result:=copy(instr,posn(';',instr,2)+1,maxint);
You forgot the last parameter for copy.
Using maxint, of course, assumes that the third column is the final column. |
If the final value is not provided in the copy command, then copy defaults to the rest of the string, so there's no difference, between, no value and maxint.
So
Code:
result:=copy(instr,posn(';',instr,2)+1);
is functionally equivalent to
Code:
result:=copy(instr,posn(';',instr,2)+1,maxint);
|

September 4th, 2012, 05:55 PM
|
|
Contributing User
|
|
Join Date: Jun 2008
Posts: 251
 
Time spent in forums: 2 Days 22 h 50 m 14 sec
Reputation Power: 5
|
|
|
Can you debug and see what value is being returned by PosN? Compare it to your input string and make sure it is returning the correct position.
|

September 4th, 2012, 08:49 PM
|
|
Registered User
|
|
Join Date: Jul 2012
Posts: 29
Time spent in forums: 4 h 36 m 52 sec
Reputation Power: 0
|
|
Here is the error I get form posn.

|

September 4th, 2012, 09:14 PM
|
|
Contributing User
|
|
Join Date: Jan 2006
Location: Carlsbad, CA
|
|
|
OK. Its much easier to help (I hope!) with the error messages,
as I have posted in this forum so many times.
The inner function needs defining before the begin of the outer function not after.
Clive
|

September 4th, 2012, 11:04 PM
|
|
Contributing User
|
|
Join Date: Jun 2008
Posts: 251
 
Time spent in forums: 2 Days 22 h 50 m 14 sec
Reputation Power: 5
|
|
As Clive mentioned you have moved the begin statement to the incorrect place.
Here is how the function(s) should look:
Code:
function ThirdColumn(InStr: String): String;
function PosN(LookFor, LookIn: String; nthPosition: Cardinal): Integer;
var
I, CurPos: Integer;
begin
CurPos := 0;
For I := 1 to nthPosition do
begin
CursPos := PosEx(LookFor, LookIn, CurPos+1);
if CurPos := 0 then
Break;
end;
Result := CurPos;
end;
begin
Result := Copy(InStr, PosN(';', InStr, 2)+1);
end;
|

September 5th, 2012, 12:41 AM
|
|
Contributing User
|
|
Join Date: Jan 2006
Location: Carlsbad, CA
|
|
|
And, in your own best interest, take heed of my advice from earlier in this thread about scoping variables.
This is not just a Delphi thing. It is a basic rule of good coding that all variables should be declared with the minimum scope
required to get the job done.
If you intend to do any amount of serious programming I guarantee that breaking this rule
will eventually cost you hours and hours of frustrating debugging.
Clive
|
Developer Shed Advertisers and Affiliates
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Rate This Thread |
Linear Mode
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|