Page 1 of 2 12 Last
  • Jump to page:
    #1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2012
    Posts
    29
    Rep 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.
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2008
    Posts
    355
    Rep Power
    7
    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.
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2012
    Posts
    29
    Rep 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....

    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.
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Regular (2000 - 2499 posts)

    Join Date
    Jan 2006
    Location
    Carlsbad, CA
    Posts
    2,057
    Rep Power
    383
    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.
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2012
    Posts
    29
    Rep Power
    0
    Including strutils header to the program, and using function, works perfectly!..

    Thank you.
  10. #6
  11. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2008
    Posts
    355
    Rep Power
    7
    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;
  12. #7
  13. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2012
    Posts
    29
    Rep Power
    0
    thank you so much.
    working perfectly.
  14. #8
  15. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2012
    Posts
    29
    Rep 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.
  16. #9
  17. No Profile Picture
    Contributing User
    Devshed Regular (2000 - 2499 posts)

    Join Date
    Jan 2006
    Location
    Carlsbad, CA
    Posts
    2,057
    Rep Power
    383
    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.
  18. #10
  19. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2008
    Posts
    355
    Rep Power
    7
    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);
  20. #11
  21. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2008
    Posts
    355
    Rep Power
    7
    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.
  22. #12
  23. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2012
    Posts
    29
    Rep Power
    0
    Here is the error I get form posn.

  24. #13
  25. No Profile Picture
    Contributing User
    Devshed Regular (2000 - 2499 posts)

    Join Date
    Jan 2006
    Location
    Carlsbad, CA
    Posts
    2,057
    Rep Power
    383
    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
  26. #14
  27. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2008
    Posts
    355
    Rep Power
    7
    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;
  28. #15
  29. No Profile Picture
    Contributing User
    Devshed Regular (2000 - 2499 posts)

    Join Date
    Jan 2006
    Location
    Carlsbad, CA
    Posts
    2,057
    Rep Power
    383
    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
Page 1 of 2 12 Last
  • Jump to page:

IMN logo majestic logo threadwatch logo seochat tools logo