Author Topic: foreach x lst to mapcar cdr lst  (Read 4486 times)

0 Members and 1 Guest are viewing this topic.

jaydee

  • Guest
foreach x lst to mapcar cdr lst
« 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

Lee Mac

  • Seagull
  • Posts: 12914
  • London, England
Re: foreach x lst to mapcar cdr lst
« Reply #1 on: October 22, 2011, 09:26:55 AM »
Maybe;

Code: [Select]
(mapcar '(lambda ( a b ) (cons (car a) b)) lst2 lst1)

jaydee

  • Guest
Re: foreach x lst to mapcar cdr lst
« Reply #2 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.

Lee Mac

  • Seagull
  • Posts: 12914
  • London, England
Re: foreach x lst to mapcar cdr lst
« Reply #3 on: October 22, 2011, 11:33:25 AM »
You're welcome jaydee  :-)

If you have any questions about the methods or code, just ask.

jaydee

  • Guest
Re: foreach x lst to mapcar cdr lst
« Reply #4 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

Lee Mac

  • Seagull
  • Posts: 12914
  • London, England
Re: foreach x lst to mapcar cdr lst
« Reply #5 on: October 22, 2011, 01:18:59 PM »
Perhaps take a look at this  :-)

CAB

  • Global Moderator
  • Seagull
  • Posts: 10401
Re: foreach x lst to mapcar cdr lst
« Reply #6 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")
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.

CAB

  • Global Moderator
  • Seagull
  • Posts: 10401
Re: foreach x lst to mapcar cdr lst
« Reply #7 on: October 22, 2011, 04:57:27 PM »
Oh, sorry Lee I should have looked at the link. We have the same answer. :oops:
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.

jaydee

  • Guest
Re: foreach x lst to mapcar cdr lst
« Reply #8 on: October 22, 2011, 07:20:19 PM »
Thankyou Cab and Lee
Another perfect answers.

Lee Mac

  • Seagull
  • Posts: 12914
  • London, England
Re: foreach x lst to mapcar cdr lst
« Reply #9 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-)

jaydee

  • Guest
Re: foreach x lst to mapcar cdr lst
« Reply #10 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
« Last Edit: October 22, 2011, 11:17:11 PM by jaydee »

jaydee

  • Guest
Re: foreach x lst to mapcar cdr lst
« Reply #11 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

CAB

  • Global Moderator
  • Seagull
  • Posts: 10401
Re: foreach x lst to mapcar cdr lst
« Reply #12 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")
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.

Lee Mac

  • Seagull
  • Posts: 12914
  • London, England
Re: foreach x lst to mapcar cdr lst
« Reply #13 on: October 23, 2011, 07:47:26 AM »
The stripmtext3.5 could not do it.
Xpress tool text2mtext could not do it.

Unformat String can do it  8-)

Code: [Select]
_$ (LM:Unformat "DBA\\\\P1\\PAAA\\PBBB\\PCCC" nil)
"DBA\\P1 AAA BBB CCC"

CAB

  • Global Moderator
  • Seagull
  • Posts: 10401
Re: foreach x lst to mapcar cdr lst
« Reply #14 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\
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.

Lee Mac

  • Seagull
  • Posts: 12914
  • London, England
Re: foreach x lst to mapcar cdr lst
« Reply #15 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?

jaydee

  • Guest
Re: foreach x lst to mapcar cdr lst
« Reply #16 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

CAB

  • Global Moderator
  • Seagull
  • Posts: 10401
Re: foreach x lst to mapcar cdr lst
« Reply #17 on: October 23, 2011, 08:00:21 AM »
OK, I get it now  :-o

Off to get more coffee......
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.

jaydee

  • Guest
Re: foreach x lst to mapcar cdr lst
« Reply #18 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"
« Last Edit: October 23, 2011, 08:38:20 AM by jaydee »

Lee Mac

  • Seagull
  • Posts: 12914
  • London, England
Re: foreach x lst to mapcar cdr lst
« Reply #19 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")

Lee Mac

  • Seagull
  • Posts: 12914
  • London, England
Re: foreach x lst to mapcar cdr lst
« Reply #20 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.

jaydee

  • Guest
Re: foreach x lst to mapcar cdr lst
« Reply #21 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.

Lee Mac

  • Seagull
  • Posts: 12914
  • London, England
Re: foreach x lst to mapcar cdr lst
« Reply #22 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: