Author Topic: List to String  (Read 10726 times)

0 Members and 1 Guest are viewing this topic.

cadman6735

  • Guest
List to String
« on: June 07, 2013, 11:18:45 AM »
Is there a way in vanilla lisp to turn a list into a string?  I have found "read" to go from string to list but not the other way around

I have also found "vl-string->list" & "vl-list->string" but I don't understand how or why to use them... they only seem to count the string or list elements.

I am interesting in both vanilla and visualLisp, if someone would give me an example...

Thanks for any help...

CAB

  • Global Moderator
  • Seagull
  • Posts: 10401
Re: List to String
« Reply #1 on: June 07, 2013, 11:27:05 AM »
Maybe
(apply 'strcat (mapcar 'chr '(65 66 67)))
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.

cadman6735

  • Guest
Re: List to String
« Reply #2 on: June 07, 2013, 11:55:17 AM »
Hi CAB, thanks for the reply

This is looks to be similar to the results of the vl-sting->list & vl-list->string

Code: [Select]
Command: (apply 'strcat (mapcar 'chr '(65 66 67)))
"ABC"


I guess I am looking for (this is a list) to turn into a string "this is a list"

CAB

  • Global Moderator
  • Seagull
  • Posts: 10401
Re: List to String
« Reply #3 on: June 07, 2013, 12:16:15 PM »
Not sure what you are getting at.

Command: (setq lst (vl-string->list "this is a list"))
(116 104 105 115 32 105 115 32 97 32 108 105 115 116)
Command: (apply 'strcat (mapcar 'chr lst))
"this is a list"

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.

cadman6735

  • Guest
Re: List to String
« Reply #4 on: June 07, 2013, 12:39:10 PM »
aaha...

Thanks CAB...  your example explained and showed me exactly what I was looking for...

Each letter has its on number and each number has its own letter...  Sometimes the examples in the help are too simple or vague to grasp the concept and I am not a programmer so the explanations (verbage) is bit on the technical side for me to understand at first glance but you got me there...

Thanks

dgorsman

  • Water Moccasin
  • Posts: 2437
Re: List to String
« Reply #5 on: June 07, 2013, 01:34:47 PM »
In case you aren't aware, have a look at ASCII character codes.

And remember that a "list" in LISP is very generic - it can be numbers, letters, vla-objects, entities, or a mix of all of them.  "Converting a list to a string" is highly dependant on what the list is composed of.
If you are going to fly by the seat of your pants, expect friction burns.

try {GreatPower;}
   catch (notResponsible)
      {NextTime(PlanAhead);}
   finally
      {MasterBasics;}

Lee Mac

  • Seagull
  • Posts: 12929
  • London, England
Re: List to String
« Reply #6 on: June 07, 2013, 03:01:44 PM »
I guess I am looking for (this is a list) to turn into a string "this is a list"

Maybe:
Code - Auto/Visual Lisp: [Select]
  1. _$ (setq lst '(this is a list))
  2. (THIS IS A LIST)
  3. "(THIS IS A LIST)"

bilançikur

  • Newt
  • Posts: 82
Re: List to String
« Reply #7 on: June 08, 2013, 01:54:12 AM »
I guess I am looking for (this is a list) to turn into a string "this is a list"

Lee, it still returns "(THIS IS A LIST)".

Maybe to remove the "(" and ")"one could use this:

Code: [Select]
(setq lst '(this is a list))
(vl-string-trim " ( ) " (vl-princ-to-string lst))
It will return "THIS IS A LIST"

CAB

  • Global Moderator
  • Seagull
  • Posts: 10401
Re: List to String
« Reply #8 on: June 08, 2013, 09:32:19 AM »
No sure if the OP is trying to use vanilla lisp or just needs to learn about the conversion functions.

Command: (setq lst (vl-string->list "this is a list"))
(116 104 105 115 32 105 115 32 97 32 108 105 115 116)
Command: (vl-list->string lst)
"this is a list"

Perhaps if we were given an example of the task.  8)
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.

irneb

  • Water Moccasin
  • Posts: 1794
  • ACad R9-2016, Revit Arch 6-2016
Re: List to String
« Reply #9 on: June 10, 2013, 07:32:16 AM »
I'm also not too sure if we're getting the idea of the OP's query. The vl-string->list and vl-list->string converts a string into a list of ASCII integers and back.

The vl-prin*-to-string converts anything into a string representation using some default settings.

Is the OP perhaps looking for a tokenizing routine? Perhaps similar to changing a list into a csv line?
Code - Auto/Visual Lisp: [Select]
  1. (defun stringlist->tokenstring  (source token /)
  2.   (apply 'strcat (cdr (apply 'append (mapcar (function (lambda (value) (list token value))) source)))))
Splitting with spaces
Code: [Select]
_$ (stringlist->tokenstring '("This" "is" "a" "list") " ")
"This is a list"
Or as would be more usually used, comma separated:
Code: [Select]
_$ (stringlist->tokenstring '("This" "is" "a" "list") ",")
"This,is,a,list"

Note this only works on lists containing strings in each item.
Common sense - the curse in disguise. Because if you have it, you have to live with those that don't.

pBe

  • Bull Frog
  • Posts: 402
Re: List to String
« Reply #10 on: June 14, 2013, 09:55:21 AM »
10 points to Irné  for the use of the word tokenizing  :)
 
 

irneb

  • Water Moccasin
  • Posts: 1794
  • ACad R9-2016, Revit Arch 6-2016
Re: List to String
« Reply #11 on: June 15, 2013, 02:32:35 AM »
10 points to Irné
Hehehe! What's that 10 percentage points?
Common sense - the curse in disguise. Because if you have it, you have to live with those that don't.

Ketxu

  • Newt
  • Posts: 109
Re: List to String
« Reply #12 on: June 15, 2013, 03:44:25 AM »
I usually use :
Code - Auto/Visual Lisp: [Select]
  1. (defun l2s(lst str)(substr (apply 'strcat (mapcar '(lambda(x)(strcat str x)) lst)) (1+ (strlen str))))

pBe

  • Bull Frog
  • Posts: 402
Re: List to String
« Reply #13 on: June 15, 2013, 10:58:01 AM »
10 points to Irné
Hehehe! What's that 10 percentage points?
In the scale of 10 of course  :) ....  you know like they use in gymnastics scoring system.  ;D
 

irneb

  • Water Moccasin
  • Posts: 1794
  • ACad R9-2016, Revit Arch 6-2016
Re: List to String
« Reply #14 on: June 15, 2013, 01:35:12 PM »
I usually use :
Code - Auto/Visual Lisp: [Select]
  1. (defun l2s(lst str)(substr (apply 'strcat (mapcar '(lambda(x)(strcat str x)) lst)) (1+ (strlen str))))
I would usually do the same, though from some other threads I've learned that the less strcat's in the loop, the faster the code runs.
Code: [Select]
_$ (setq test nil)
nil
_$ (length (repeat 10000 (setq test (cons "test" test))))
10000
_$ (strlen (stringlist->tokenstring test ","))
49999
_$ (strlen (l2s test ","))
49999
_$ (quickbench '((stringlist->tokenstring test ",") (l2s test ",")))
Benchmarking .. done for 64 iterations. Sorted from fastest.
Statement                                Increment  Time(ms) Normalize  Relative
--------------------------------------------------------------------------------
(STRINGLIST->TOKENSTRING TEST ,)                64      1077      1077      1.27
(L2S TEST ,)                                    64      1372      1372      1.00
--------------------------------------------------------------------------------
_$ (setq test nil)
nil
_$ (length (repeat 1000 (setq test (cons "test" test))))
1000
_$ (strlen (stringlist->tokenstring test ","))
4999
_$ (strlen (l2s test ","))
4999
_$ (quickbench '((stringlist->tokenstring test ",") (l2s test ",")))
Benchmarking .. done for 1024 iterations. Sorted from fastest.
Statement                                Increment  Time(ms) Normalize  Relative
--------------------------------------------------------------------------------
(STRINGLIST->TOKENSTRING TEST ,)              1024      1684      1684      1.26
(L2S TEST ,)                                   512      1061      2122      1.00
--------------------------------------------------------------------------------
_$ (setq test nil)
nil
_$ (length (repeat 10 (setq test (cons "test" test))))
10
_$ (strlen (stringlist->tokenstring test ","))
49
_$ (strlen (l2s test ","))
49
_$ (quickbench '((stringlist->tokenstring test ",") (l2s test ",")))
Benchmarking .. done for 16384 iterations. Sorted from fastest.
Statement                                Increment  Time(ms) Normalize  Relative
--------------------------------------------------------------------------------
(STRINGLIST->TOKENSTRING TEST ,)             16384      1123      1123      1.00
(L2S TEST ,)                                 16384      1123      1123      1.00
--------------------------------------------------------------------------------
Especially with long lists.  :wink:
Common sense - the curse in disguise. Because if you have it, you have to live with those that don't.