Jump to content

Recommended Posts

Posted

Hello lisp guru's
I'm a bit stuck, well to be blunt i haven't even starting writing any code yet, i know the issue i'm going to come up against so i'm trying to problem solve before even getting into it.

 

I want to get some data from a string, and in my mind i'm thinking how would i even start with cadr etc in this scenario, well here's where i'm stumped.
The string doesn't have spaces, its continuous, and where a space would normally be it uses a | character for separation.
Well i've no idea how i get some data from within this kind of string, with | as the separation character. Here's a rough example
Lets say the string is this (and bear in mind these bits of data between the | do vary):

 

"-1|NCC|2.54|30|0|Auto|0|1|0|2|0|0|2|0|False|0|0"  (or another to show how it can vary "1|CCR|2.0|37.5|0|User|2|0|0|25|0|0|0|0|True|0|0)"

The string is always divided into this many sections (17), the data between can vary but I only ever want to to retrieve the 3rd section of data, in this case the "2.54"
and add it to a variable, still as a string, no need to change it to a real for my needs.
Could someone point me in the right direction? 

Posted

like this?

; (SplitStr "a,b" ",") -> ("a" "b")
(defun SplitStr (s d / p)
  (if (setq p (vl-string-search d s))(cons (substr s 1 p)(SplitStr (substr s (+ p 1 (strlen d))) d))(list s)))

(nth 2 (splitstr "-1|NCC|2.54|30|0|Auto|0|1|0|2|0|0|2|0|False|0|0"  "|"))

 

  • Like 2
  • Agree 1
Posted

The string-handling functions in AutoLISP are not the best. car won't help unless you have a list.

 

So let's make a list. Add quotes at beginning and end. Replace each pipe with quote-space-quote. Pull out the third element. Done.

  • Thanks 1
Posted

Here's another:

(mapcar
  'vl-princ-to-string
  (read
    (strcat "(" (vl-string-translate "|" " " "-1|NCC|2.54|30|0|Auto|0|1|0|2|0|0|2|0|False|0|0") ")")
  )
)
;; ("-1" "NCC" "2.54" "30" "0" "AUTO" "0" "1" "0" "2" "0" "0" "2" "0" "FALSE" "0" "0") 

 

  • Like 2
Posted (edited)

You could look at Lee Macs string to List function, use the | character as the deliminator: https://lee-mac.com/stringtolist.html

 

(almost the same as what RLX has)

 

 

Edited by Steven P
  • Thanks 1
Posted

I do like ronjonp's version , its a few characters less code and I'm a BIG fan of short(er) code for my tiny lisp toolbox 🤓

Posted (edited)

I am a big fan of Lee Mac make a list as suggested.

 

; tab 9 space 32 comma 44 semicolum 59 | 124

; thanks to Lee-mac for this defun
(defun csv->lst ( str / pos )
(if (setq pos (vl-string-position 32 str))
    (cons (substr str 1 pos) (csv->lst (substr str (+ pos 2))))
    (list str)
    )
)

I often rename to suit delimeter.

; tab 9 space 32 comma 44 semicolum 59 | 124 : 58

; thanks to Lee-mac for this defun
(defun csv->lst124 ( str / pos )
(if (setq pos (vl-string-position 124 str))
    (cons (substr str 1 pos) (csv->lst124 (substr str (+ pos 2))))
    (list str)
    )
)

 

Edited by BIGAL
Posted (edited)
4 hours ago, rlx said:

I do like ronjonp's version , its a few characters less code and I'm a BIG fan of short(er) code for my tiny lisp toolbox 🤓

:) One limitation I've found is if one of the the strings start with a period "|.54|". Then it errors out on "misplaced dot on input"
 

Edited by ronjonp
Posted
2 hours ago, ronjonp said:

:) One limitation I've found is if one of the the strings start with a period "|.54|". Then it errors out on "misplaced dot on input"
 

oh dear.... that's unfortunate 🥴 , awel , Lee's method is best... like always

Posted
6 hours ago, rlx said:

Lee's method is best... like always

That's usually the case! :beer:

  • Funny 1
Posted

Guys thanks a lot for all or your posts, i will look into them all.
I'm sorry for the delay responding, my hard drive failed on my system about an hour after i posted, and i'm currently reinstalling all my software. (Happy days).

 

You guys are the best, love how you bounce off each other
 

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...