Remove (text) from a List

Remove (text) from a List

DGCSCAD
Collaborator Collaborator
935 Views
12 Replies
Message 1 of 13

Remove (text) from a List

DGCSCAD
Collaborator
Collaborator

How would I remove (CENTER) from this list?

 

List:

(CT5625062-1 (CENTER) NO NO TUBE)

AutoCad 2018 (full)
Win 11 Pro
0 Likes
Accepted solutions (1)
936 Views
12 Replies
Replies (12)
Message 2 of 13

ВeekeeCZ
Consultant
Consultant

If it's '("CT5625062-1 (CENTER) NO NO TUBE") use vl-string-subst

Message 3 of 13

hak_vz
Advisor
Advisor

Here you have one option that checks all text entities inside list

(SETQ
	LST '
		(
			"CT5625062-1 (CENTER) NO NO TUBE" 
			"CT5625062-1 (CENTER) NO NO TUBE"
			"CT5625062-1 (CENTER) NO NO TUBE"
		)
	LST (MAPCAR '(lambda (x)(vl-string-subst "" " (CENTER)" x 0)) lst)
)
; result  ("CT5625062-1 NO NO TUBE" "CT5625062-1 NO NO TUBE" "CT5625062-1 NO NO TUBE")

Miljenko Hatlak

EESignature

Did you find this post helpful? Feel free to Like this post.
Did your question get successfully answered? Then click on the ACCEPT SOLUTION button.
Message 4 of 13

john.kaulB9QW2
Advocate
Advocate

Command: (vl-remove '(CENTER) '(CT5625062-1 (CENTER) NO NO TUBE))
(CT5625062-1 NO NO TUBE)

 

Didn't I make a beginner programming challenge for something like this at TheSwamp as well?

another swamper
Message 5 of 13

DGCSCAD
Collaborator
Collaborator

@john.kaulB9QW2 wrote:

Command: (vl-remove '(CENTER) '(CT5625062-1 (CENTER) NO NO TUBE))
(CT5625062-1 NO NO TUBE)

 

Didn't I make a beginner programming challenge for something like this at TheSwamp as well?


I think you may have, many moons ago. 🙂

 

Thank you for the replies. I should have mentioned that the (CENTER) changes to other characters and is variable, but there's always alphabetical characters within the ( and ).

 

If that makes sense?

I'd need to use wildcards (*).

AutoCad 2018 (full)
Win 11 Pro
0 Likes
Message 6 of 13

john.kaulB9QW2
Advocate
Advocate

'variable', 'wildcards', 'nested list' are red-flags that you should be asking a different question. What are you doing? Why are you removing items from a list (can you ignore instead)?  -i.e. I have tried to demonstrate that more often than not you can typically get around "sorting" a list (because these types of operations are expensive); so that last question is/can be "what are you trying to do to each item in the list"?

another swamper
0 Likes
Message 7 of 13

DGCSCAD
Collaborator
Collaborator

I 'm taking a string and cross-checking for a match in a comma delimited text file. The only common denominator between 1500 different strings is their "type", which is always the 2nd element in the returned list after matching the string. Some of the returned lists have (text), which throws off my nth count for those lists, but other than that they are consistent.

 

I have found a solution using an if statement (see below) which accounts for each situation and is consistent throughout, but I'd still like to see how to resolve the original predicament. For science purposes.

 

Code snippet:

(findfile (setq TextFile "mytxtfile.txt"))
(setq SearchStr (strcat "Part1" ","))
(setq Opened (open TextFile "r"))
	(while (setq tmpLine (read-line Opened))
		(if (vl-string-search SearchStr tmpLine)
			(setq endlist tmpLine)
		)
	)
(close Opened)
(if (/= endlist nil)
	(progn
		(setq term_type1_list (read (strcat "(" (vl-string-translate "," " " endlist) ")")))
		(setq term_list1_lngth (length term_type1_list))
		(if (= term_list1_lngth 4)
			(setq term_type1 (nth 1 term_type1_list))
			(setq term_type1 (nth 2 term_type1_list))
		)
         )
)

 

AutoCad 2018 (full)
Win 11 Pro
0 Likes
Message 8 of 13

komondormrex
Mentor
Mentor
Accepted solution

(setq _list '(CT5625062-1 (CENTER) NO NO TUBE))

(vl-remove (cadr _list) _list) 

0 Likes
Message 9 of 13

DGCSCAD
Collaborator
Collaborator

@komondormrex wrote:

(setq _list '(CT5625062-1 (CENTER) NO NO TUBE))

(vl-remove (cadr _list) _list) 


As simple as that. 🙂

 

Thank you komondormrex.

AutoCad 2018 (full)
Win 11 Pro
0 Likes
Message 10 of 13

john.kaulB9QW2
Advocate
Advocate

@DGCSCAD wrote:

I 'm taking a string and cross-checking for a match in a comma delimited text file. The only common denominator between 1500 different strings is their "type", which is always the 2nd element in the returned list after matching the string. Some of the returned lists have (text), which throws off my nth count for those lists, but other than that they are consistent.

 

I have found a solution using an if statement (see below) which accounts for each situation and is consistent throughout, but I'd still like to see how to resolve the original predicament. For science purposes.

 

--- >%


Sorry, but it sounds like you may be making a tempest in a teacup.  See my post on Self-commenting code.

-i.e. can't you just assign names to those positions and not really worry about "contents"?

-e.g.

( (lambda ( / one-half
              numer denom make )
    (set 'numer car)
    (set 'denom cdr)
    (set 'make cons)
    (setq one-half (make 1 2))
    (princ (strcat "\nNumerator: " (itoa (numer one-half))))
    (princ (strcat "\nDenominator: " (itoa (denom one-half))))

    (if (eq (type (numer one-half)) 'STRING)
      "Our fraction is broken!"
      )

    (princ)
    )
 )

 

another swamper
Message 11 of 13

Kent1Cooper
Consultant
Consultant

@DGCSCAD wrote:
As simple as that. ....

Another simple way to remove the second item, whatever it is, from any List:

(cons (car TheList) (cddr TheList))

 

That has the advantage, if the situation arises, that if that second item is one of more than one occurrence of the same thing in the list, this will remove only the one in the second position, whereas (vl-remove) will remove all occurrences of the same entry.

Kent Cooper, AIA
Message 12 of 13

Sea-Haven
Mentor
Mentor

Another re message 5 always "(text)".

 

(setq str "CT5625062-1 (CENTER) NO NO TUBE")
(setq pos1 (vl-string-search "(" str))
(setq pos2 (vl-string-search ")" str))
(setq str (strcat (substr str 1 (- pos1 1)) (substr str (+ pos2 2))))

"CT5625062-1 NO NO TUBE"

Another  

Message 13 of 13

DGCSCAD
Collaborator
Collaborator

@john.kaulB9QW2 wrote:


Sorry, but it sounds like you may be making a tempest in a teacup.  See my post on Self-commenting code.

-i.e. can't you just assign names to those positions and not really worry about "contents"?

-e.g.

 

( (lambda ( / one-half
              numer denom make )
    (set 'numer car)
    (set 'denom cdr)
    (set 'make cons)
    (setq one-half (make 1 2))
    (princ (strcat "\nNumerator: " (itoa (numer one-half))))
    (princ (strcat "\nDenominator: " (itoa (denom one-half))))

    (if (eq (type (numer one-half)) 'STRING)
      "Our fraction is broken!"
      )

    (princ)
    )
 )

 

 


I would love to be able to include elegant coding such as this, but I'd need to study and understand it. I kind of get the gist of it, but since I'm under the gun for solutions right now I'll have to tuck this away until I have more time to make sure it all sinks in. I appreciate the insight though John. Thank you. 

AutoCad 2018 (full)
Win 11 Pro
0 Likes