Delphi Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsProgramming Languages - MoreDelphi Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old August 23rd, 2012, 05:57 AM
turkalpk turkalpk is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jul 2012
Posts: 29 turkalpk User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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.

Reply With Quote
  #2  
Old August 23rd, 2012, 09:25 AM
majlumbo majlumbo is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jun 2008
Posts: 251 majlumbo User rank is Lance Corporal (50 - 100 Reputation Level)majlumbo User rank is Lance Corporal (50 - 100 Reputation Level)majlumbo User rank is Lance Corporal (50 - 100 Reputation Level) 
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.

Reply With Quote
  #3  
Old August 26th, 2012, 04:14 PM
turkalpk turkalpk is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jul 2012
Posts: 29 turkalpk User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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.

Reply With Quote
  #4  
Old August 26th, 2012, 05:49 PM
clivew clivew is offline
Contributing User
Dev Shed Regular (2000 - 2499 posts)
 
Join Date: Jan 2006
Location: Carlsbad, CA
Posts: 2,045 clivew User rank is Major (30000 - 40000 Reputation Level)clivew User rank is Major (30000 - 40000 Reputation Level)clivew User rank is Major (30000 - 40000 Reputation Level)clivew User rank is Major (30000 - 40000 Reputation Level)clivew User rank is Major (30000 - 40000 Reputation Level)clivew User rank is Major (30000 - 40000 Reputation Level)clivew User rank is Major (30000 - 40000 Reputation Level)clivew User rank is Major (30000 - 40000 Reputation Level)clivew User rank is Major (30000 - 40000 Reputation Level)clivew User rank is Major (30000 - 40000 Reputation Level) 
Time spent in forums: 1 Week 6 Days 2 h 37 m
Reputation Power: 382
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.

Reply With Quote
  #5  
Old August 27th, 2012, 01:32 PM
turkalpk turkalpk is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jul 2012
Posts: 29 turkalpk User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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.

Reply With Quote
  #6  
Old August 27th, 2012, 01:46 PM
majlumbo majlumbo is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jun 2008
Posts: 251 majlumbo User rank is Lance Corporal (50 - 100 Reputation Level)majlumbo User rank is Lance Corporal (50 - 100 Reputation Level)majlumbo User rank is Lance Corporal (50 - 100 Reputation Level) 
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;

Reply With Quote
  #7  
Old August 28th, 2012, 09:34 AM
turkalpk turkalpk is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jul 2012
Posts: 29 turkalpk User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 4 h 36 m 52 sec
Reputation Power: 0
thank you so much.
working perfectly.

Reply With Quote
  #8  
Old September 4th, 2012, 03:36 PM
turkalpk turkalpk is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jul 2012
Posts: 29 turkalpk User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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.

Reply With Quote
  #9  
Old September 4th, 2012, 04:50 PM
clivew clivew is offline
Contributing User
Dev Shed Regular (2000 - 2499 posts)
 
Join Date: Jan 2006
Location: Carlsbad, CA
Posts: 2,045 clivew User rank is Major (30000 - 40000 Reputation Level)clivew User rank is Major (30000 - 40000 Reputation Level)clivew User rank is Major (30000 - 40000 Reputation Level)clivew User rank is Major (30000 - 40000 Reputation Level)clivew User rank is Major (30000 - 40000 Reputation Level)clivew User rank is Major (30000 - 40000 Reputation Level)clivew User rank is Major (30000 - 40000 Reputation Level)clivew User rank is Major (30000 - 40000 Reputation Level)clivew User rank is Major (30000 - 40000 Reputation Level)clivew User rank is Major (30000 - 40000 Reputation Level) 
Time spent in forums: 1 Week 6 Days 2 h 37 m
Reputation Power: 382
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.

Reply With Quote
  #10  
Old September 4th, 2012, 05:46 PM
majlumbo majlumbo is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jun 2008
Posts: 251 majlumbo User rank is Lance Corporal (50 - 100 Reputation Level)majlumbo User rank is Lance Corporal (50 - 100 Reputation Level)majlumbo User rank is Lance Corporal (50 - 100 Reputation Level) 
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); 

Reply With Quote
  #11  
Old September 4th, 2012, 05:55 PM
majlumbo majlumbo is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jun 2008
Posts: 251 majlumbo User rank is Lance Corporal (50 - 100 Reputation Level)majlumbo User rank is Lance Corporal (50 - 100 Reputation Level)majlumbo User rank is Lance Corporal (50 - 100 Reputation Level) 
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.

Reply With Quote
  #12  
Old September 4th, 2012, 08:49 PM
turkalpk turkalpk is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jul 2012
Posts: 29 turkalpk User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 4 h 36 m 52 sec
Reputation Power: 0
Here is the error I get form posn.


Reply With Quote
  #13  
Old September 4th, 2012, 09:14 PM
clivew clivew is offline
Contributing User
Dev Shed Regular (2000 - 2499 posts)
 
Join Date: Jan 2006
Location: Carlsbad, CA
Posts: 2,045 clivew User rank is Major (30000 - 40000 Reputation Level)clivew User rank is Major (30000 - 40000 Reputation Level)clivew User rank is Major (30000 - 40000 Reputation Level)clivew User rank is Major (30000 - 40000 Reputation Level)clivew User rank is Major (30000 - 40000 Reputation Level)clivew User rank is Major (30000 - 40000 Reputation Level)clivew User rank is Major (30000 - 40000 Reputation Level)clivew User rank is Major (30000 - 40000 Reputation Level)clivew User rank is Major (30000 - 40000 Reputation Level)clivew User rank is Major (30000 - 40000 Reputation Level) 
Time spent in forums: 1 Week 6 Days 2 h 37 m
Reputation Power: 382
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

Reply With Quote
  #14  
Old September 4th, 2012, 11:04 PM
majlumbo majlumbo is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jun 2008
Posts: 251 majlumbo User rank is Lance Corporal (50 - 100 Reputation Level)majlumbo User rank is Lance Corporal (50 - 100 Reputation Level)majlumbo User rank is Lance Corporal (50 - 100 Reputation Level) 
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;

Reply With Quote
  #15  
Old September 5th, 2012, 12:41 AM
clivew clivew is offline
Contributing User
Dev Shed Regular (2000 - 2499 posts)
 
Join Date: Jan 2006
Location: Carlsbad, CA
Posts: 2,045 clivew User rank is Major (30000 - 40000 Reputation Level)clivew User rank is Major (30000 - 40000 Reputation Level)clivew User rank is Major (30000 - 40000 Reputation Level)clivew User rank is Major (30000 - 40000 Reputation Level)clivew User rank is Major (30000 - 40000 Reputation Level)clivew User rank is Major (30000 - 40000 Reputation Level)clivew User rank is Major (30000 - 40000 Reputation Level)clivew User rank is Major (30000 - 40000 Reputation Level)clivew User rank is Major (30000 - 40000 Reputation Level)clivew User rank is Major (30000 - 40000 Reputation Level) 
Time spent in forums: 1 Week 6 Days 2 h 37 m
Reputation Power: 382
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

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming Languages - MoreDelphi Programming > Extract desirable column from .txt file... how....

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap