Services & Resources / Wolfram Forums
MathGroup Archive
*Archive Index
*Ask about this page
*Print this page
*Give us feedback
*Sign up for the Wolfram Insider

MathGroup Archive 2005

[Date Index] [Thread Index] [Author Index]

Search the Archive

Re: Re: enumerating list items

  • To: mathgroup at
  • Subject: [mg56054] Re: [mg55986] Re: [mg55976] enumerating list items
  • From: "Wolf, Hartmut" <Hartmut.Wolf at>
  • Date: Thu, 14 Apr 2005 08:55:20 -0400 (EDT)
  • Sender: owner-wri-mathgroup at


>-----Original Message-----
>From: Søren Merser [mailto:merser at] 
To: mathgroup at
>Sent: Wednesday, April 13, 2005 7:10 AM
>Subject: [mg56054] [mg55986] Re: [mg55976] enumerating list items
>thanks for your reply
>what i really want is:
>i'm getting data from a database link, including column headers
>assigning  1 : ncols to these names  should enable call column[data, 
>(or better: assign each data column to it's header name)
>regards søren
>----- Original Message ----- 
>From: "Wolf, Hartmut" <Hartmut.Wolf at>
To: mathgroup at
>Subject: [mg56054] [mg55986] Re: [mg55976] enumerating list items
>>-----Original Message-----
>>From: Søren Merser [mailto:merser at]
To: mathgroup at
>>Sent: Tuesday, April 12, 2005 11:27 AM
>>Subject: [mg56054] [mg55986] [mg55976] enumerating list items
>>is there a way to make each item in the list 'assignable',
>>i.e. i want to
>>enumerate the items like id=1, gender=2 etc.
>>a={"id", "gender", "status", "odag"}
>>regards soren
>You have to convert the strings to symbols, before you can 
>assign to them. 
>This works, provided the symbols already don't exist with values:
>In[1]:= a={"id","gender","status","odag"};
>In[2]:= MapIndexed[(Evaluate[Symbol[#1]]=First[#2])&,a];
>In[3]:= ?status
>        Global`status
>        status = 3
>In[4]:= Clear/@ a;    (* to repeat that, we first have to 
>clear the symbols 
>In[5]:= MapThread[Set,{Symbol/@a , Range[Length[a]]}];
>In[6]:= ?odag
>        Global`odag
>        odag = 4
>The question as how to proceed when you want to change values, 
>and you only 
>have the names at hand, is not trivial but a FAQ, see the 
>archive, if you 
>are interested.
>Good advice to you depends on what you really want to do. If 
>you just want 
>to associate your "names" with values you might better proceed 
>(assoc here is the name of your association):
>In[7]:= Thread[Evaluate[assoc /@ a] = Range[Length[a]]]
>Out[7]= {1, 2, 3, 4}
>This retrieves a "value":
>In[8]:= assoc["gender"]
>Out[8]= 2
>This re-assigns a "value":
>In[9]:= assoc["gender"] = 999
>Out[9]= 999
>Hartmut Wolf


let's make an example:

data = {{2000367, "m", "ok", 7945},
        {2000792, "f", "not ok", 233},
        {2000877, "f", "most ok", 1}};

Now define an access function "relation" (corresponding to your DB-relation or table, i.e. you define a function for each DB-relation -- you seem to ahve only one -- not for each attribute):

MapThread[(relation[data_, row_, #1] := data[[row, #2]]) &, {a, 

With that you can retrieve any field, here "gender" of row 2:

In[12]:= relation[data, 2, "gender"]
Out[12]= "f"

Or a whole column:

In[13]:= relation[data, All, "odag"]
Out[13]= {7945, 233, 1}

In other words you need not associate values with your names of the  columns, but with the relation itself.  Then you may use the strings (names) itself as "indices".

Of course you may define a function that retrieves the total column

MapThread[(columnOfRelation[data_, #1] := data[[All, #2]]) &, {a, 

In[17]:= columnOfRelation[data, "odag"]
Out[17]= {7945, 233, 1}

In[18]:= columnOfRelation[data, "id"]
Out[18]= {2000367, 2000792, 2000877}

Hartmut Wolf

  • Prev by Date: Re: Infinite sum of gaussians
  • Next by Date: Re: Effect of Simplify on numeric vs symbolic
  • Previous by thread: Re: enumerating list items
  • Next by thread: Infinite sum of gaussians