SKILL Q: How does foreach evaluate?

Discussion in 'Cadence' started by Svenn Are Bjerkem, Nov 4, 2005.

  1. Hi,

    I am having a small problem:

    wave1 = (clip VT("/sig1N") - VT("/sig1P") 10n 20n)
    wave2 = (clip VT("/sig2N") - VT("/sig2P") 10n 20n)
    wave3 = (clip VT("/sig3N") - VT("/sig3P") 10n 20n)
    setq of (outfile "~/outfile.txt" "w")
    (foreach family (list wave1 wave2 wave3)
    (fprintf of "%L:\n" family)
    (foreach sweepVal (famGetSweepValues family)
    (fprintf of "%L\n" (cross (famValue family sweepVal) 0 0 'either)))

    What I would like to see in my output file is:
    wave1:
    (3.591667e-08 3.716667e-08 3.841667e-08 3.966667e-08)
    (3.591667e-08 3.716667e-08 3.841667e-08 3.966667e-08)
    (3.591667e-08 3.716667e-08 3.841667e-08 3.966667e-08)
    (3.591667e-08 3.716667e-08 3.841667e-08 3.966667e-08)
    wave2:
    (3.591667e-08 3.716667e-08 3.841667e-08 3.966667e-08)
    (3.591667e-08 3.716667e-08 3.841667e-08 3.966667e-08)
    (3.591667e-08 3.716667e-08 3.841667e-08 3.966667e-08)
    (3.591667e-08 3.716667e-08 3.841667e-08 3.966667e-08)
    wave3:
    (3.591667e-08 3.716667e-08 3.841667e-08 3.966667e-08)
    (3.591667e-08 3.716667e-08 3.841667e-08 3.966667e-08)
    (3.591667e-08 3.716667e-08 3.841667e-08 3.966667e-08)
    (3.591667e-08 3.716667e-08 3.841667e-08 3.966667e-08)

    but instead of "wave1" I get drwave:196834416 etc.

    Which means that I would like to have the name of the variable instead
    of the contents printed on the first fprintf line, but I guess that the
    variables wave1, wave2 and wave3 has been replaced with their respective
    names already in the first foreach line. How would I go on to delay the
    substitution until the second foreach line?
     
    Svenn Are Bjerkem, Nov 4, 2005
    #1
  2. Svenn Are Bjerkem

    Jimka Guest

    HI Swenn, by default foreach returns the list it iterated over. this
    is because it is the most memory efficient thing to return. the list
    it itererated over is already allocated memory, and simply a pointer to
    it is returned. you can use the return value as a boolean to know
    whether any iteration occured, but you cannot use the default return
    value to know what values were calculated.

    there is an optional first argument to foreach which defaults to mapc,
    but you can use any of several built in mapping functions, mapcar,
    mapcan, map, maplist (perhaps others but i've forgotten). the mapping
    function tells two things,1) what the return value should be, and 1)
    which values should the iteration variable take on.

    1) mapc ==> iteration value takes on each element of the list, and the
    same list is returned
    (foreach mapc x '( 1 2 3)
    (fun x))
    the list (1 2 3) is returned

    (unless (foreach x (dbGetOpenCellViews)
    (println (list x~>libName
    x~>cellName
    x~>viewName)))
    (printf "no cellviews are open\n"))


    2) mapcar ==> iteration takes on each element of the list, and the list
    of calculated values is returned
    (foreach mapcar x '(1 2 3)
    2 * x)
    the list (2 4 6) is returned

    3) map ==> iteration takes on successive cons-cells of the list, and
    sorry i've forgotten what is returned.
    (foreach map x '(1 2 3)
    (println x))
    prints the following
    (1 2 3)
    (2 3)
    (3)


    4) mapcan ==> iteration takes on successives values of the list and
    each calculated value must be a list, the return value is nconc applied
    to the list of returned values, the result is that the lists are
    appeneded together very efficiently.
    (foreach mapcan x '( 1 2 3 4 5 6 7)
    (when (oddp x)
    (list x)))

    returns (1 3 5 7)
     
    Jimka, Nov 5, 2005
    #2
  3. Jim gave a useful summary of the various mapping operators in SKILL,
    but to answer your specific question you could do:

    (foreach family '(wave1 wave2 wave3)
    (fprintf of "%s\n" family)
    (setq famData (symeval family))
    (foreach sweepVal (famGetSweepValues famData)
    (fprintf ... famData ...)
    )
    )

    In this case, family will be set to the symbols wave1, wave2, wave3,
    and then the symeval will get the value of the corresponding symbo.

    The downside of the above is that it's a bit awkward if you're using SKILL++
    which has lexical scoping - the symeval would need a second argument which
    is the lexical environment you're in. Another approach would be to use a macro -
    rather than me explaining that now, search on google for a previous posting I
    made explaining macros - I think I used the words in the posting "macros aren't
    difficult, everyone just thinkgs they are" - if searching for this proves
    difficult. Sorry, but I'm on a plane, otherwise I'd look it up.

    Regards,

    Andrew.
     
    Andrew Beckett, Nov 9, 2005
    #3
Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.