Author Topic: Comma Delineated file (survey data) string-reading or sorting  (Read 1822 times)

0 Members and 1 Guest are viewing this topic.

James Cannon

  • Guest
I have a file with varying number of lines, as you'd expect, for survey data, always comma delineated .asc with such content as shown at the end of the post.

I have done something similar in PHP of all things, where I extract data from a file that was space-delineated rather than comma... but now I need to do it in either LSP, VBA, or (if I'm feeling gutsy) use it as a "starter" project to help learn C#.NET though I think I'll wait for a better example to start on .NET unless someone advises that this would be a good start.

Anyways... to give overall scope and context: the end result would be inserting an attributed block at the proper coordinate location, with values = to what they should be for:
(point #)
(description [sometimes none])

My assumption at going about the code is to take it line by line, read the first line, extract first text bit before comma1 and send that value to the variable (XLOC) for example.. then the text between comma1 and comma2 and send that value to variable (YLOC) and so on... then when done sending the text after comma4 to (DESC)

I would run the subroutine that would set the variable values to the appropriate attributed-block values, insert the block (or maybe in the other order, actually) and then loop back until the file is out of lines to read. 

I am REASONABLY sure I can handle the block insertion/creation part, once I have the reading part handled... but after 10-15 minutes of searching, I found not too much on comma-delineated reading because it seems everyone who needs this... has Land Desktop or C3D or something :P  Or maybe that just dominated my search results *shrug*

I'm sorry I haven't been able to start up on anything for this part of the routine... I really am not sure where it is to start.  Any help (even a push to a good example/reference for whatever function will help me would be appreciated) would be very much loved.

Code: [Select]
1,482833.5680,2643153.3620,2.7700,OPUS HORIZ
2,482904.3768,2643119.6492,1.5300,1" I.PIPE
5,482905.1864,2642523.1998,1.8300,FD 1" I.P.
6,482902.0794,2642513.8652,3.0176,60d NAIL
7,482776.3324,2643179.2332,3.0345,CL RD
8,482848.5453,2643020.1402,2.3977,CL RD
9,482876.6448,2642930.1160,2.2111,CL RD
10,482881.1293,2642847.5440,1.8695,CL RD
11,482862.7124,2642741.3819,2.0542,CL RD
12,482870.5581,2642674.7530,2.1052,CL RD
13,482907.3438,2642644.4060,2.6012,CL RD
14,483277.1749,2642594.3174,3.5855,CONC PILE
15,483460.3159,2642632.9590,3.6224,CONC PILE
16,483421.2991,2642724.4525,3.0549,WOOD PILE
17,483282.6350,2642776.0969,6.9742,WOOD PILE
18,483197.7012,2642744.4158,6.9882,WOOD PILE
19,482969.9102,2642795.5905,6.7244,WOOD PILE


  • Global Moderator
  • Seagull
  • Posts: 10401
Re: Comma Delineated file (survey data) string-reading or sorting
« Reply #1 on: March 19, 2009, 10:16:28 AM »
Look at this routine:

It reads all the lines from a data file.
Then you need this subroutine to separate the data.
Code: [Select]
;  parser by CAB single character delim, match ","
(defun sparser (str delim / ptr lst)
  (while (setq ptr (vl-string-search delim str))
    (setq lst (cons (substr str 1 ptr) lst))
    (setq str (substr str (+ ptr 2)))
  (reverse(cons str lst))
I've reached the age where the happy hour is a nap. ()
Windows 10 core i7 4790k 4Ghz 32GB GTX 970
Please support this web site.


  • Seagull
  • Posts: 17750
  • Have thousands of dwgs to process? Contact me.
Re: Comma Delineated file (survey data) string-reading or sorting
« Reply #2 on: March 19, 2009, 10:17:49 AM »
I don't have time to put together a proper tut, or even document the following. However, it is pretty trivial, so maybe it will help illuminate:

Code: [Select]
(defun _FileToList ( filename / path handle result )

    ;;  Convert a text file to a list of strings.

    (if (setq path (findfile filename))
        (if (setq handle (open path "r"))
            (while (setq stream (read-line handle))
                (setq result

    (if handle (close handle))

    (reverse result)


Code: [Select]
(defun _CsvToList ( csv )

    ;;  Convert a CSV (Comma Separated Values) string
    ;;  to a list of strings.

    (   (lambda ( lst / result word )
            (foreach code (reverse lst)
                (if (eq 44 code)
                    (setq result (cons word result) word nil)
                    (setq word (cons code word))
            (mapcar 'vl-list->string
                (cons word result)
        (vl-string->list csv)

Code: [Select]
(defun c:Test ( / fileAsList csvData fields )

    (if (setq fileAsList (_FileToList "c:/docs/datum.csv"))
            (setq csvData (mapcar '_CsvToList fileAsList))
            (setq fields '(ID X Y Z DESC))
            (foreach record csvData
                (mapcar 'set fields '(nil nil nil nil nil))
                (mapcar 'set fields record)
                (foreach field fields
                    (princ field)
                    (princ "=")
                    (princ (eval field))
                    (if (null (eq field (last fields))) (princ ","))                       
                (princ "\n")



ID=1,X=482833.5680,Y=2643153.3620,Z=2.7700,DESC=OPUS HORIZ
ID=2,X=482904.3768,Y=2643119.6492,Z=1.5300,DESC=1" I.PIPE
ID=5,X=482905.1864,Y=2642523.1998,Z=1.8300,DESC=FD 1" I.P.
ID=6,X=482902.0794,Y=2642513.8652,Z=3.0176,DESC=60d NAIL
ID=7,X=482776.3324,Y=2643179.2332,Z=3.0345,DESC=CL RD
ID=8,X=482848.5453,Y=2643020.1402,Z=2.3977,DESC=CL RD
ID=9,X=482876.6448,Y=2642930.1160,Z=2.2111,DESC=CL RD
ID=10,X=482881.1293,Y=2642847.5440,Z=1.8695,DESC=CL RD
ID=11,X=482862.7124,Y=2642741.3819,Z=2.0542,DESC=CL RD
ID=12,X=482870.5581,Y=2642674.7530,Z=2.1052,DESC=CL RD
ID=13,X=482907.3438,Y=2642644.4060,Z=2.6012,DESC=CL RD
ID=14,X=483277.1749,Y=2642594.3174,Z=3.5855,DESC=CONC PILE
ID=15,X=483460.3159,Y=2642632.9590,Z=3.6224,DESC=CONC PILE
ID=16,X=483421.2991,Y=2642724.4525,Z=3.0549,DESC=WOOD PILE
ID=17,X=483282.6350,Y=2642776.0969,Z=6.9742,DESC=WOOD PILE
ID=18,X=483197.7012,Y=2642744.4158,Z=6.9882,DESC=WOOD PILE
ID=19,X=482969.9102,Y=2642795.5905,Z=6.7244,DESC=WOOD PILE

Edit: Fixed pooched formatting.
« Last Edit: March 19, 2009, 10:41:03 AM by MP »
Engineering Technologist CAD Automation Practitioner
Automation ▸ Design ▸ Drafting ▸ Document Control ▸ Client

James Cannon

  • Guest
Re: Comma Delineated file (survey data) string-reading or sorting
« Reply #3 on: March 19, 2009, 10:25:24 AM »
Both of you:  Thank you so much.

Reading through it, there are parts I do not understand automatically, which gives me somewhere to start!

And surely, MP, looking at  your output that is -exactly- the method I need to accomplish.

While you were posting, I was looking up how to get the user to select the file to read, and how to loop through each line, and that combined with what I have before me will definitely get me going!  I know I've seen good examples before for filling out attributed blocks so if I have trouble there, I think I know I'll have good material to help me already.

Again, thank you both for the help, I know it might be low level to you guys, but as far as LSP goes, I'm at best a hack'n'slash code-assembler :oops: lol  Starting from scratch is very difficult for me.  I learn as I go, though, but it's been a long time since I've been able to get my feet wet in it since... so much of what I had accomplished before has gotten rusty (use it or lose it I guess)

Thanks again!


  • Seagull
  • Posts: 17750
  • Have thousands of dwgs to process? Contact me.
Re: Comma Delineated file (survey data) string-reading or sorting
« Reply #4 on: March 19, 2009, 10:39:53 AM »
If I may "James" ... try to get in the habit of dividing a solution into discrete functions: functions that do only one thing. Too often I see solutions that are monolithic which doesn't lend itself to (1) easy debugging / maintenance (2) code reuse. The solution's wrapper (in the example I provided c:Test) then brings the individual functions (e.g. _FileToList & CsvToList) together to assemble the solution.

/soap box

PS: When converting a string value to a numerical one, say in the case of the X,Y,Z values, you may find this helpful / a starting point:

Code: [Select]
(defun _DistOf ( x / result fixed )

[color=green]    ;;  Convert a string to a numerical value if possible. Also, if it
    ;;  can be determined that the intent of the string representation
    ;;  is that the number is to be an integer rather than an a real an
    ;;  integer is returned.
       '(lambda ( base ) (setq result (distof x base)))
       '(2 1 5 3 4)

    (if result
        (if (vl-string-position 46 x)
            result [color=green]    ;; assume result is a real[/color]
            (if (eq result (setq fixed (fix result)))
                fixed  [color=green];; assume result is an integer[/color]
                result [color=green];; assume result is a real[/color]
Engineering Technologist CAD Automation Practitioner
Automation ▸ Design ▸ Drafting ▸ Document Control ▸ Client