SKILL: how to convert from a struct to association list

M

m774

Guest
What's the best and what's the easiest way to convert a struct to an
association list?

Say I have a structure named s. I can access the property list with s-
??, which returns a list containing (name1 value1 name2 value2 ... ).
However I need an association list: ((name1 value1) (name2
value2) ...)

I thought a function exist to perform this basic (i.e. converting from
data-structures), but couldn't find anything in the documentation.

Thanks for any hint!
 
Hi m774,

Try this (assuming your structure is myStruct):

let( (myList)
foreach( prop myStruct->?
myList = cons( list(prop myStruct->prop) myList )
)
myList ; output
)
 
Hi m774,

I don't know of any built-in Skill function that does what your are
asking.
You could try a foreach/mapcar.
Say:
ISW> defstruct(mosfet w l nf modelName)
ISW> nmosStruct=make_mosfet(?w "1u" ?l "0.1u" ?nf "2" ?modelName
"nmosfet")
Then:
ISW> nmosAssocList=foreach( mapcar prop nmosStruct~>? list(prop get
(nmosStruct prop)))
will return the Assc List you are looking for:

((modelName "nmosfet")
(nf "2")
(l "0.1u")
(w "1u")
)

BTW, the '??' builds a DPL out of the structure, i.e.
nmosDPL=nmosStruct~>??

(modelName "nmosfet" nf "2" l
"0.1u" w "1u"
)

This might be a quick solution If you don't mind DPLs ...

You could go for Eyn's otherwise ...

Cheers,
Riad.
 
On Aug 10, 10:26 pm, eyn <dream...@gmail.com> wrote:
Hi m774,

Try this (assuming your structure is myStruct):

let( (myList)
foreach( prop myStruct->?
  myList = cons( list(prop myStruct->prop) myList )
)
myList ; output
)
Thanks. This works, but if you modify how you access the prop value:
myStruct->prop could be changed into get(myStruct prop), as in Riad
code.
 
On Aug 10, 11:14 pm, Riad KACED <riad.ka...@gmail.com> wrote:
Hi m774,

I don't know of any built-in Skill function that does what your are
asking.
You could try a foreach/mapcar.
Say:
ISW> defstruct(mosfet w l nf modelName)
ISW> nmosStruct=make_mosfet(?w "1u" ?l "0.1u" ?nf "2" ?modelName
"nmosfet")
Then:
ISW> nmosAssocList=foreach( mapcar prop nmosStruct~>? list(prop get
(nmosStruct prop)))
will return the Assc List you are looking for:

((modelName "nmosfet")
    (nf "2")
    (l "0.1u")
    (w "1u")
)

BTW, the '??' builds a DPL out of the structure, i.e.
nmosDPL=nmosStruct~>??

(modelName "nmosfet" nf "2" l
    "0.1u" w "1u"
)

This might be a quick solution If you don't mind DPLs ...

You could go for Eyn's otherwise ...

Cheers,
Riad.
Thank you all. Both code are ok for me. the code by Eyn retrieve the
properties in reverse order, but I don't care here.
Thanks a lot!
 
Hi,

The reverse order in Eyn's comes out the 'cons' function. cons adds
elements to the front of the list, giving a result that is “back to
front”. You might consider using 'tconc' if the order really matters.
tconc is more efficient than using 'reverse'.

Cheers,
Riad.
 

Welcome to EDABoard.com

Sponsor

Back
Top