Sharper Posted July 1 Posted July 1 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? Quote
rlx Posted July 1 Posted July 1 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" "|")) 2 1 Quote
CyberAngel Posted July 1 Posted July 1 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. 1 Quote
ronjonp Posted July 1 Posted July 1 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") 2 Quote
Steven P Posted July 1 Posted July 1 (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 July 1 by Steven P 1 Quote
rlx Posted July 1 Posted July 1 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 Quote
BIGAL Posted July 2 Posted July 2 (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 July 2 by BIGAL Quote
ronjonp Posted July 2 Posted July 2 (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 July 2 by ronjonp Quote
rlx Posted July 2 Posted July 2 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 Quote
ronjonp Posted July 2 Posted July 2 6 hours ago, rlx said: Lee's method is best... like always That's usually the case! 1 Quote
Sharper Posted July 3 Author Posted July 3 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 Quote
Recommended Posts
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.