3
$\begingroup$

I had an Excel spreadsheet with a list of symbols names in the first column and a list of values in the second column. I imported it. Working with the resulting expression, I now have (in Mathematica) a list of names and a list of corresponding values.

names[[1 ;; 44]]
{"avogadro", "kgPerAmu", "gasConstant"...

values[[1 ;; 44]]
{6.02214*10^23, 1.66054*10^-27, 8.31446

What I want is a package with "assignement statements."

avogadro=6.23
boltzman=59

etc. I have no idea how to convert a pair of lists into a series of expressions. In fact, I don't know what functions to look up in the documentation. How to get my constants into a package?

$\endgroup$
1
  • $\begingroup$ I think it is covered by the generic topic I've linked, let me know if you disagree with closing. $\endgroup$
    – Kuba
    Commented Aug 7, 2018 at 7:29

2 Answers 2

1
$\begingroup$

You can try this:

MapThread[
 ToExpression["Set[" <> #1 <> "," <> ToString[#2] <> "]"] &,
 {names, values}
 ]
$\endgroup$
1
  • 1
    $\begingroup$ Or ToExpression@StringTemplate["``=``"][##] & $\endgroup$
    – Kuba
    Commented Aug 7, 2018 at 7:39
2
$\begingroup$

Also:

MapThread[Set, {Symbol /@ names, values}] (* or *)
Set @@@ Transpose[{Symbol /@ names, values}]
$\endgroup$
3
  • $\begingroup$ OK, MapThread[ ToExpression["Set[" <> #1 <> "," <> ToString[#2] <> "]"] &, {namesd, valuesd}] worked, but how do I make the symbols permanent? It is my experience that symbols in Mathematica just go away after a few days. It was a big deal creating the lists of names and symbols. I don't want to have to repeat it every day. $\endgroup$
    – Gene Naden
    Commented Aug 7, 2018 at 7:07
  • 2
    $\begingroup$ @GeneNaden Unless you save them they will go away, take a look at this topic and other topics linked there: mathematica.stackexchange.com/q/121/5478 As well as documentation for Save/DumpSave etc. $\endgroup$
    – Kuba
    Commented Aug 7, 2018 at 7:36
  • $\begingroup$ Yeah, I used Save command and it created a file with the lists in it, so that solved it. $\endgroup$
    – Gene Naden
    Commented Aug 8, 2018 at 14:50

Not the answer you're looking for? Browse other questions tagged or ask your own question.