MathGroup Archive 2009

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

Search the Archive

Re: Basic questions on list manipulation in the

  • To: mathgroup at smc.vnet.net
  • Subject: [mg95596] Re: [mg95541] Basic questions on list manipulation in the
  • From: DrMajorBob <btreat1 at austin.rr.com>
  • Date: Thu, 22 Jan 2009 07:01:57 -0500 (EST)
  • References: <200901211145.GAA12433@smc.vnet.net>
  • Reply-to: drmajorbob at longhorns.com

For instance,

ct = {{sym1 -> {val1 -> 20, val2 -> 300,
       val3 -> 1000}}, {sym2 -> {val1 -> 50, val2 -> 500,
       val3 -> 20000}}};

changeVal[ct_, who_, class_, amt_] :=
  ct /. Rule[who, {a___, Rule[class, _], b___}] :>
    Rule[who, {a, Rule[class, amt], b}]

ct = changeVal[ct, sym2, val1, "hello"]

{{sym1 -> {val1 -> 20, val2 -> 300,
     val3 -> 1000}}, {sym2 -> {val1 -> "hello", val2 -> 500,
     val3 -> 20000}}}

If you don't like the assignment construct "ct = changeVal[..]", an  
alternative is

ct = {{sym1 -> {val1 -> 20, val2 -> 300,
       val3 -> 1000}}, {sym2 -> {val1 -> 50, val2 -> 500,
       val3 -> 20000}}};

Clear[changeVal]
SetAttributes[changeVal, HoldFirst]
changeVal[ct_, who_, class_, amt_] :=
  ct = ct /.
    Rule[who, {a___, Rule[class, _], b___}] :>
     Rule[who, {a, Rule[class, amt], b}]

changeVal[ct, sym2, val1, "hello"];
ct

{{sym1 -> {val1 -> 20, val2 -> 300,
     val3 -> 1000}}, {sym2 -> {val1 -> "hello", val2 -> 500,
     val3 -> 20000}}}

If you want changeVal to treat ct as a global, not an argument (never  
recommended), then:

ct = {{sym1 -> {val1 -> 20, val2 -> 300,
       val3 -> 1000}}, {sym2 -> {val1 -> 50, val2 -> 500,
       val3 -> 20000}}};

Clear[changeVal]
changeVal[who_, class_, amt_] :=
  ct = ct /.
    Rule[who, {a___, Rule[class, _], b___}] :>
     Rule[who, {a, Rule[class, amt], b}]

changeVal[sym2, val1, "hello"];
ct

{{sym1 -> {val1 -> 20, val2 -> 300,
     val3 -> 1000}}, {sym2 -> {val1 -> "hello", val2 -> 500,
     val3 -> 20000}}}

Bobby

On Wed, 21 Jan 2009 05:45:28 -0600, <dangerrity at gmail.com> wrote:

> Hello,
>
> I have some basic questions about using lists in Mathematica.
> Specifically I have a table that serves as sort of a database with the
> following structure:
>
> ct = {
>           { sym1 -> {val1->20, val2->300, val3->1000, ... },
>           { sym2 -> {val1->50, val2->500, val3->20000,...},
>           ...
>        }
>
> sym1, sym2, ... are people and val1, val2 ... represent attributes of
> each person.
>
> Now I'm trying to go in and modify values.  I can do it, but I think
> not well and I think with a "programming language" mindset instead of
> a "Mathematica mindset."
>
> Here is my specific question.  In order to change a specific value in
> the list above (valxxx) for a given individual (symxxx), I created
> this function:
>
> changeVal[ who_, class_, amnt_ ] := (
>     ct[[ Position[ ct, who ][[ 1, 1 ]], 2 ]] =
>     ReplacePart[ ct[[ Position[ ct, who ][[ 1, 1 ]], 2 ]],
>      Position[ ct[[ Position[ ct, who ][[ 1, 1 ]] ]], class ][[ 1,
> 2 ]] -> (class -> amnt) ]
> );
>
> Now I know there is a better way than that using list manipulation and
> patterns.  Can some of you experienced pros help me out?  I call this
> "write only" code because I don't know that I could explain it once
> it's written.
>
> Perhaps a more fundamental question: is this the right way to store
> data in lists?  Or would it be better to just have the values and
> reference them by index number?
>
> Thanks for your patience with a simple question.
>
>



-- 
DrMajorBob at longhorns.com


  • Prev by Date: Re: Re: Re: Which editor do you use for math articles
  • Next by Date: Re: Debugging Mathematica Code (Mathematica 7)
  • Previous by thread: Re: ï
  • Next by thread: Re: Basic questions on list manipulation in the