Jump to content

Recommended Posts

Guest kruuger
Posted

hello

i wrote something like this. it reads line from file and split it into 3:

(defun C:TEST (/ FL)
 (setq FL (getfiled "SelectFile" "*." "txt" )
 (princ (kr:SplitList (kr:ReadFile FL)))
 (princ)
)
(defun kr:ReadFile (File / OFL L LST)
 (if (findfile File)
   (if (setq OFL (open File "r"))
     (progn
       (setq L "")
       (while (/= L :EOF)
         (setq LST (cons (setq L (read-line OFL)) LST))
       )
       (close OFL)
       (reverse (cdr LST))
     )
   )
   (alert (strcat "Can't find file " File "."))
 )
)
(defun kr:SplitList (In / LST)
 (foreach % In
   (setq LST
     (cons
       (list
         (substr % 1 10)
         (substr % 10 16)
         (substr % 26 5)
       )
       LST
     )
   )
 )
 LST
)

everything works ok but i want practice mapcar-lambda function so i try to rewrite SplitList function. it is working but returns wrong list. code below

(defun kr:SplitList2 (In / LST 3L)
 (mapcar
  '(lambda (%)
     (setq LST
       (cons
         (mapcar
          '(lambda (%1)
             (setq 3L (append 3L (list (substr % (car %1) (last %1)))))
           )
           (list '(1 10) '(10 16) '(26 5))
         )
         LST
       )
     )
   )
   In
 )
 LST
)

can someone help with this?

 

thanks

kruuger

Posted

(defun kr:SplitList2 (In / LST 3L)
 (mapcar
  '(lambda (%)
     (setq LST
       (cons
         (mapcar
          '(lambda (%1)
             (setq 3L(substr % (car %1) (last %1)))
           )
           (list '(1 10) '(10 16) '(26 5))
         )
         LST
       )
     )
   )
   In
 )
 LST
)

 

(append 3L (list no need because mapcar creates list by himself.

Posted

And without extra variables:

 

(defun kr:SplitList3 (In)
 (mapcar
  '(lambda (%)
       (list
         (mapcar
          '(lambda (%1)
             (substr % (car %1) (last %1))
           )
           (list '(1 10) '(10 16) '(26 5))
         )
       )
   )
   In
 )
)

 

:)

Guest kruuger
Posted

Beauty:) Thanks Smirnoff

like this is perfect:

(defun kr:SplitList3 (In)
 (mapcar
  '(lambda (%)
     (mapcar
      '(lambda (%1)
         (substr % (car %1) (last %1))
       )
       (list '(1 10) '(10 16) '(26 5))
     )
   )
   In
 )
)

kruuger

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.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...