Author Topic: foreach x lst to mapcar cdr lst  (Read 4491 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.