• Skip to main content
  • Skip to search
  • Skip to footer
Cadence Home
  • This search text may be transcribed, used, stored, or accessed by our third-party service providers per our Cookie Policy and Privacy Policy.

  1. Community Forums
  2. Custom IC SKILL
  3. Search and replace in a sub list

Stats

  • Locked Locked
  • Replies 3
  • Subscribers 143
  • Views 17199
  • Members are here 0
This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

Search and replace in a sub list

WTony
WTony over 7 years ago

Hi

I would like to do a search and replace in a sub list.

suppose I have : myList = list(list("a" "b") list("c" "d"))

the list can also be myList = list(list("c" "d" "e") list("a" "b" "f"))

the sublist length is not constant

I don't know in advance where the character is in the list. the only Information I have is the variable "myList", the character I need to search "b" and the string I need to replace "newB"

how can I replace "b" by "newB"

result:

 myList =(("a" "newB") ("c" "d")) or myList = (("c" "d" "e") ("a" "newB" "f"))

thanks for help

  • Cancel
Parents
  • WTony
    WTony over 7 years ago

    I found a solution buy I'm not sure it's optimal.

    foreach(subList myList
       if( member("b" subList) then
        rplaca(member("b" subList) "newB")
       )
      ) 

    • Cancel
    • Vote Up 0 Vote Down
    • Cancel
Reply
  • WTony
    WTony over 7 years ago

    I found a solution buy I'm not sure it's optimal.

    foreach(subList myList
       if( member("b" subList) then
        rplaca(member("b" subList) "newB")
       )
      ) 

    • Cancel
    • Vote Up 0 Vote Down
    • Cancel
Children
  • Andrew Beckett
    Andrew Beckett over 7 years ago in reply to WTony

    Two more general solutions. These work with any depth of lists, with any length of the sublists, and change all occurrences of the original value with the new value. The first is non-destructive - i.e. it constructs a new list with the same structure and doesn't disturb the original list:

    ;------------------------------------------------------------------------
    ; Non-destructive version - constructs a new list
    ;------------------------------------------------------------------------
    procedure(CCFreplaceDeep(theList orig new "lgg")
        foreach(mapcar elem theList
            cond(
                (listp(elem) CCFreplaceDeep(elem orig new))
                (elem==orig new)
                (t elem)
            )
        )
    )

    The second is a destructive version - it changes the original list in-place. So it's more memory efficient (creates less garbage) but it has the side-effect of modifying the original list:

    ;------------------------------------------------------------------------
    ; Destructive version - replaces list elements in place
    ;------------------------------------------------------------------------
    procedure(CCFreplaceDeepInPlace(theList orig new "lgg")
        foreach(map remainder theList
            cond(
                (listp(car(remainder))
                    CCFreplaceDeepInPlace(car(remainder) orig new))
                (car(remainder)==orig rplaca(remainder new))
            )
        )
        theList
    )

    Regards,

    Andrew.

    • Cancel
    • Vote Up 0 Vote Down
    • Cancel
  • WTony
    WTony over 7 years ago in reply to Andrew Beckett

    thanks Andrew, I'll try it

    • Cancel
    • Vote Up 0 Vote Down
    • Cancel

Community Guidelines

The Cadence Design Communities support Cadence users and technologists interacting to exchange ideas, news, technical information, and best practices to solve problems and get the most from Cadence technology. The community is open to everyone, and to provide the most value, we require participants to follow our Community Guidelines that facilitate a quality exchange of ideas and information. By accessing, contributing, using or downloading any materials from the site, you agree to be bound by the full Community Guidelines.

© 2025 Cadence Design Systems, Inc. All Rights Reserved.

  • Terms of Use
  • Privacy
  • Cookie Policy
  • US Trademarks
  • Do Not Sell or Share My Personal Information