Thanks Udo, but this is not quite what I’m looking for.
The reason I inculded the pattern is because I’d like to avoid using indexing - as soon as my pattern changes your routine will have to be updated as well.
If this is used in say 20 additional functions, all of them will have to be rewritten
This is not a very intelligent solution either, as it introduces a lot of global variables and on top of that it creates a lot of confusion if there’s another construct for example, which requires a name with some value other than 1.
The solution that I’m looking for should not introduce any global vars and should figure out the part to be updated from the pattern PPerson. This way if/when PPerson changes the routine will not require modification
To avoid indexing, make the code immune to (some) changes in fields, avoid global variables, avoid “magic constants”, the classic solution in programming languages is to use “named fields.” So instead of
to make the changes you want made. You give your symbols the Orderless attribute so it doesn’t matter what order the pairs are given in and I think you have satisfied at least all the requirements you have listed thus far, but I expect more shortly.
@Toder If you wrap the SetName operation in Trace you can see what’s going on. In the first case, `Name` is a local symbol because you’re using it inside a function that has Person[Name_String, …] as a pattern. So {Name → “George”} becomes {“John” → “George”}). In the second scenario `Name` remains a raw symbol.
There’s a lot of ways to approach your general goal. If you want to keep the plain indexed approach you can try a function like this:
Which grabs the indices from the PPerson pattern itself. Though I think using rules (Person[Name → “Bob”, Phone → “xxx-xxxx”, …]) is nice overall because you can quickly grab data like this:
Name <> " is " <> ToString[Age] <> " years old" /. (List @@ person)