Author Topic: Help (text extraction)  (Read 5210 times)

0 Members and 1 Guest are viewing this topic.

Mark

  • Custom Title
  • Seagull
  • Posts: 28762
Help (text extraction)
« on: January 29, 2004, 11:17:03 AM »
I'm working on a program that reads a file and extracts certain info from each line. How would you go about doing this?

This is one of the lines, the 'G 01' is my indicator, I need only the 'RNL15' part of the line stored in a variable. The 'RNL15' part is _not_ a constant, it may contain just numbers or just text.

G 01 RNL15                     P F2 NL
TheSwamp.org  (serving the CAD community since 2003)

hendie

  • Guest
Help (text extraction)
« Reply #1 on: January 29, 2004, 11:25:45 AM »
wouldn't you really need to know / identify what the constants are first ? by that I mean, is the part you're after always after the second space ? is it always "X" characters long ? is it always between the second and third spaces ? or is there some other characteristic that you can identify it by ?
without some constants in the text I don't see how you could do it with code.

daron

  • Guest
Help (text extraction)
« Reply #2 on: January 29, 2004, 11:30:44 AM »
See if anything in this will help you figure it out.
Code: [Select]
(defun c:newlist ()
     (setq readfile (open (findfile "newlisps.txt") "r"))
     (setq cmdlist (open "c://cmdlist.txt" "w"))
     (while (setq line (read-line readfile))
 (setq lispfile (open (findfile (strcat line ".lsp")) "r"))
 (write-line (strcat line ".lsp") cmdlist)
 (while (setq align (read-line lispfile))
      (if (wcmatch (strcase align) "*C:*")
   (progn
(setq func (substr align 10))
(if (wcmatch func "*(*")
     (progn
  (setq counter 1)
  (while (/= (substr func counter 1) "(")
(setq counter (1+ counter))
  ) ;_ while
  (setq cmdname (substr func 1 (1- counter)))
     ) ;_ progn
     (setq cmdname func)
) ;_ if
(write-line
     (strcat "  " cmdname (chr 9) (chr 9) (chr 9))
     cmdlist
) ;_ write-line
   ) ;_ progn
      ) ;_ if
 ) ;_ while
 (close lispfile)
     ) ;_ while
     (close readfile)
     (close cmdlist)
) ;_ defun

JohnK

  • Administrator
  • Seagull
  • Posts: 10637
Help (text extraction)
« Reply #3 on: January 29, 2004, 11:30:58 AM »
Real rough:
I would create a list of the line entry.
Cycle thru each char and test each with a condition --True or False.
Build a list of all the "true" test results.
Return the list.

This method would work if the "go1" would always be the same number of char's and the "rn15" is always the same number of char's.
TheSwamp.org (serving the CAD community since 2003)
Member location map - Add yourself

Donate to TheSwamp.org

JohnK

  • Administrator
  • Seagull
  • Posts: 10637
Help (text extraction)
« Reply #4 on: January 29, 2004, 11:31:40 AM »
Holly crap you guys answer fast!
TheSwamp.org (serving the CAD community since 2003)
Member location map - Add yourself

Donate to TheSwamp.org

Mark

  • Custom Title
  • Seagull
  • Posts: 28762
Help (text extraction)
« Reply #5 on: January 29, 2004, 11:35:37 AM »
Quote from: hendie
wouldn't you really need to know / identify what the constants are first ? by that I mean, is the part you're after always after the second space ? is it always "X" characters long ? is it always between the second and third spaces ? or is there some other characteristic that you can identify it by ?
without some constants in the text I don't see how you could do it with code.

True hendie, I have left out part of the puzzle. G<space>01<space> is constant, you will start reading from the 5th character.
TheSwamp.org  (serving the CAD community since 2003)

Mark

  • Custom Title
  • Seagull
  • Posts: 28762
Help (text extraction)
« Reply #6 on: January 29, 2004, 11:40:43 AM »
This is what I have right now, haven't done a lot of testing and it looks kinda clumsy.
Code: [Select]

(defun StrParse1 (str / cntr s out)
  (setq cntr 0)
  (while
    (not (= (setq s (vl-string-elt str cntr)) 32))
    (setq out (cons s out)
 cntr (1+ cntr)
 )
    (vl-list->string (reverse out))
    )
  )
TheSwamp.org  (serving the CAD community since 2003)

hendie

  • Guest
Help (text extraction)
« Reply #7 on: January 29, 2004, 11:44:29 AM »
in that case, all you have to do is search for the position of the third space (chr 32),
Quote
(vl-string-position char-code str [start-pos [from-end-p]])
count the characters in between and then use substr to return the portion you want. (I'm sure there's a vl equivalent but can't think of one at the moment)
is that what you're after ?

Mark

  • Custom Title
  • Seagull
  • Posts: 28762
Help (text extraction)
« Reply #8 on: January 29, 2004, 11:50:49 AM »
In the code I posted above I strip off the first 5 char's before I send it to the StrParse1
TheSwamp.org  (serving the CAD community since 2003)

Mark

  • Custom Title
  • Seagull
  • Posts: 28762
Help (text extraction)
« Reply #9 on: January 29, 2004, 12:09:35 PM »
Or....... assuming I have already stripped off the first 5 char's (G 01 ).
Code: [Select]
(substr str 1 (vl-string-search " " str))
TheSwamp.org  (serving the CAD community since 2003)

daron

  • Guest
Help (text extraction)
« Reply #10 on: January 29, 2004, 12:17:47 PM »
Did my code do anything for you? It does what you're thinking. In mine it reads a list of lisp files I have to write. (I know there are ways to read the contents of a folder, but this was written before vl-functions). Anyway, if you look, you'll see it looks for *C:* and finds everything on that line after, then strips off everything after (. Sounds like something similar to what you are looking for.

Mark

  • Custom Title
  • Seagull
  • Posts: 28762
Help (text extraction)
« Reply #11 on: January 29, 2004, 12:33:37 PM »
I got it! I was trying to make things harder than they really are.
TheSwamp.org  (serving the CAD community since 2003)