TheSwamp

Code Red => AutoLISP (Vanilla / Visual) => Topic started by: jaydee on October 22, 2011, 09:22:13 AM

Title: foreach x lst to mapcar cdr lst
Post by: jaydee on October 22, 2011, 09:22:13 AM
Hi.
Please show me how to do this
Lst1 '("aa" "bb" "cc" "n")
lst2  '(("1" . "V1")("2" . "V2")("3" . "V3")("4" . "V4"))
--->
(("1" . "aa")("2" . "bb")("3" . "cc")("4" . "n"))

thankyou
Title: Re: foreach x lst to mapcar cdr lst
Post by: Lee Mac on October 22, 2011, 09:26:55 AM
Maybe;

Code: [Select]
(mapcar '(lambda ( a b ) (cons (car a) b)) lst2 lst1)
Title: Re: foreach x lst to mapcar cdr lst
Post by: jaydee on October 22, 2011, 10:01:58 AM
Thankyou Lee
This is perfect.
I had tried a few combinations and some are very close to yours.
One thing im keep thinking is the (cdr (assoc  of the lst2.
Your one liner give me great insight of trying to understand the code.
Title: Re: foreach x lst to mapcar cdr lst
Post by: Lee Mac on October 22, 2011, 11:33:25 AM
You're welcome jaydee  :-)

If you have any questions about the methods or code, just ask.
Title: Re: foreach x lst to mapcar cdr lst
Post by: jaydee on October 22, 2011, 12:39:29 PM
Now i got another issue convert mtext paragraph string with enter return on each line

("Abc\\pefg\\pPPP")
--->
("abc" "efg" "PPP")

thanks
Title: Re: foreach x lst to mapcar cdr lst
Post by: Lee Mac on October 22, 2011, 01:18:59 PM
Perhaps take a look at this (http://lee-mac.com/stringtolist.html)  :-)
Title: Re: foreach x lst to mapcar cdr lst
Post by: CAB on October 22, 2011, 04:54:19 PM
Maybe this:
Code: [Select]
;  parser by CAB multi char delim, match "xyz"
(defun sparser (str delim / dlen ptr lst)
  (setq dlen (1+ (strlen delim)))
  (while (setq ptr (vl-string-search delim str))
    (setq lst (cons (substr str 1 ptr) lst))
    (setq str (substr str (+ ptr dlen)))
  )
  (reverse(cons str lst))
)

Code: [Select]
(sparser "Abc\\pefg\\pPPP" "\\p")
("Abc" "efg" "PPP")
Title: Re: foreach x lst to mapcar cdr lst
Post by: CAB on October 22, 2011, 04:57:27 PM
Oh, sorry Lee I should have looked at the link. We have the same answer. :oops:
Title: Re: foreach x lst to mapcar cdr lst
Post by: jaydee on October 22, 2011, 07:20:19 PM
Thankyou Cab and Lee
Another perfect answers.
Title: Re: foreach x lst to mapcar cdr lst
Post by: Lee Mac on October 22, 2011, 07:35:47 PM
Oh, sorry Lee I should have looked at the link. We have the same answer. :oops:

No worries Alan, the more the merrier  8-)
Title: Re: foreach x lst to mapcar cdr lst
Post by: jaydee on October 22, 2011, 08:52:44 PM
Hi Lee and Cab
How to get around a string that looks like this
"
DBA\P1
AAA
BBB
CCC
"
Code: [Select]
(setq lst '("DBA\\\\P1\\PAAA\\PBBB\\PCCC"))
(sparser lst "\\P")
===>
("DBA\\" "1" "AAA" "BBB" "CCC")
Code: [Select]
(LM:str->lst lst "\\P")
===>
("DBA\\" "1" "AAA" "BBB" "CCC")
prefer result
Code: [Select]
("DBA\P1" "AAA" "BBB" "CCC")
Thankyou
Title: Re: foreach x lst to mapcar cdr lst
Post by: jaydee on October 23, 2011, 04:59:39 AM
Refer to my previous post.
Just relise its a sticky issue.
The stripmtext3.5 could not do it.
Xpress tool text2mtext could not do it.

My way of getting around the "\P" in the string is do a find and replace \P to /P before using the program.

Thankyou
Title: Re: foreach x lst to mapcar cdr lst
Post by: CAB on October 23, 2011, 07:45:07 AM
This is almost the correct result except the double \\ should be a single \
You would need to replace \\ with \ to finish the job.
Code: [Select]
(setq lst '("DBA\\\\P1\\PAAA\\PBBB\\PCCC"))
(sparser lst "\\P")
===>
("DBA\\" "1" "AAA" "BBB" "CCC")
Title: Re: foreach x lst to mapcar cdr lst
Post by: Lee Mac on October 23, 2011, 07:47:26 AM
The stripmtext3.5 could not do it.
Xpress tool text2mtext could not do it.

Unformat String (http://lee-mac.com/unformatstring.html) can do it  8-)

Code: [Select]
_$ (LM:Unformat "DBA\\\\P1\\PAAA\\PBBB\\PCCC" nil)
"DBA\\P1 AAA BBB CCC"
Title: Re: foreach x lst to mapcar cdr lst
Post by: CAB on October 23, 2011, 07:50:42 AM
Maybe I missed something but I thought this is correct for removing \\P
("DBA\\" "1" "AAA" "BBB" "CCC")
The first group in plain text should be DBA\
Title: Re: foreach x lst to mapcar cdr lst
Post by: Lee Mac on October 23, 2011, 07:54:59 AM
Maybe I missed something but I thought this is correct for removing \\P
("DBA\\" "1" "AAA" "BBB" "CCC")
The first group in plain text should be DBA\

If I've understood, the OP wishes to split the MText string by each line; however, if the "\P" is used within the MText content, it is prefixed with backslashes to mark it as a 'literal' and not be used as a new-line character code.

That is, unless I have misunderstood?
Title: Re: foreach x lst to mapcar cdr lst
Post by: jaydee on October 23, 2011, 07:58:59 AM
Thanks cab and lee.
What i meant was if the string actually contain the string with back slash follow by letter P.
Ie. ABC\P1 as the content.. not the \\P  as for mtext format enter

Cheers
Title: Re: foreach x lst to mapcar cdr lst
Post by: CAB on October 23, 2011, 08:00:21 AM
OK, I get it now  :-o

Off to get more coffee......
Title: Re: foreach x lst to mapcar cdr lst
Post by: jaydee on October 23, 2011, 08:04:28 AM
Thanks cab and lee
Lee is on the dot.

\P  is being use in the string literally as it appear in the screen.

Now i just remember, in the past i have used xpress tool text to mtext alot on older drawing created with dtext. And now i wonder where some of the letter "P" are gone.
That exactly what happen when convert dtext string "Abc\P1" = "Abc\1"
Title: Re: foreach x lst to mapcar cdr lst
Post by: Lee Mac on October 23, 2011, 08:52:19 AM
How about this instead:

Code: [Select]
(defun MultilineParser ( str / sub )
    (defun sub ( str pos )
        (if (setq pos (vl-string-search "\\P" str pos))
            (if
                (and
                    (< 0 pos)
                    (eq "\\" (substr str pos 1))
                )
                (sub (strcat (substr str 1 (1- pos)) (substr str (1+ pos))) (1+ pos))
                (cons (substr str 1 pos) (sub (substr str (+ pos 3)) 0))
            )
            (list str)
        )
    )
    (sub str 0)
)

(It will remove the extra "\" marking the "\P" as a literal):

Code: [Select]
_$ (MultilineParser "DBA\\\\P1\\PAAA\\PBBB\\PCCC")
("DBA\\P1" "AAA" "BBB" "CCC")
Title: Re: foreach x lst to mapcar cdr lst
Post by: Lee Mac on October 23, 2011, 09:02:30 AM
Now i just remember, in the past i have used xpress tool text to mtext alot on older drawing created with dtext. And now i wonder where some of the letter "P" are gone.
That exactly what happen when convert dtext string "Abc\P1" = "Abc\1"

This will happen with many strings - any string that contains the equivalent of MText formatting codes, which are not prefixed with backslashes to mark them as literals.

For example, create DText with the string:

Code: [Select]
C:\Computer\LISP
Now use the TXT2MTXT Express Tools function on that DText.  :wink:

I describe this behaviour in more detail here (http://lee-mac.com/gettruecontent.html).
Title: Re: foreach x lst to mapcar cdr lst
Post by: jaydee on October 23, 2011, 08:34:24 PM
Thankyou Lee for your help
I admit i dont come across formatted mtext codes use as actual text content, but i rather be safe.
Title: Re: foreach x lst to mapcar cdr lst
Post by: Lee Mac on October 24, 2011, 05:56:17 AM
Thankyou Lee for your help
I admit i dont come across formatted mtext codes use as actual text content, but i rather be safe.

You're welcome :-)

Just be aware of the issue when using backslashes in DText  :wink: