MathGroup Archive 2005

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

Search the Archive

Re: Re: enumerating list items

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

 

>-----Original Message-----
>From: Søren Merser [mailto:merser at image.dk] 
To: mathgroup at smc.vnet.net
>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, 
>colum-index-by-name]
>(or better: assign each data column to it's header name)
>regards søren
>
>----- Original Message ----- 
>From: "Wolf, Hartmut" <Hartmut.Wolf at t-systems.com>
To: mathgroup at smc.vnet.net
>Subject: [mg56054] [mg55986] Re: [mg55976] enumerating list items
>
>
>
>>-----Original Message-----
>>From: Søren Merser [mailto:merser at image.dk]
To: mathgroup at smc.vnet.net
>>Sent: Tuesday, April 12, 2005 11:27 AM
>>Subject: [mg56054] [mg55986] [mg55976] enumerating list items
>>
>>hi
>>
>>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"}
>>
>>b=Range@Length@a
>>
>>a=b
>>
>>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
>
>Or
>
>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 
>differently 
>(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
>
>
>
>

Søren,

let's make an example:

In[10]:=
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):

In[11]:=
MapThread[(relation[data_, row_, #1] := data[[row, #2]]) &, {a, 
    Range[Length[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

In[16]:=
MapThread[(columnOfRelation[data_, #1] := data[[All, #2]]) &, {a, 
    Range[Length[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