1
$\begingroup$

I've got a piece of working code but am trying to significantly shorten it and am out of my depth. In specific, I'm trying to force an operation on several elements on a list using the following code:

PointListT = {{0, RBase}, {Middle, RMiddle}, {Height, RTop}};
ReplacePart[PointListT, {#, 2}, PointListT[[#, 2]] - GlassThick] & /@ {1, 2, 3}

But this does not work, ideas?

$\endgroup$
3
  • 1
    $\begingroup$ If the code doesn't work, and you haven't specified what you're trying to do, we can't help! Can you explain what you are trying to accomplish and what the desired output would be for a sample input? $\endgroup$
    – march
    Commented Dec 8, 2016 at 17:14
  • 1
    $\begingroup$ You probably meant ReplacePart[list, a -> b] and not ReplacePart[list, a, b]. There is a three-argument form, but it is undocumented and the values you passed don't fit it. Stick to the documented form. Also, ReplacePart doesn't modify the list in-place. It returns a modified list. $\endgroup$
    – Szabolcs
    Commented Dec 8, 2016 at 17:17
  • $\begingroup$ If you want to subtract GlassThick from the first element of each sublist, use {#1 - GlassThick, #2}& @@@ list. MapAt[# - GlassThick &, PointListT, {{All, 1}}] will also work. Try not to use capitalized names like GlassThick and PointListT because they may collide with builtins. Use names starting with a lowercase letter. $\endgroup$
    – Szabolcs
    Commented Dec 8, 2016 at 17:20

1 Answer 1

2
$\begingroup$
pointListT = {{0, RBase}, {Middle, RMiddle}, {Height, RTop}};

Doing it with replacement.

pointListT /. {a_, b_} -> {a, b - GlassThick}]

Doing it with Apply at level 1

{#1, #2 - GlassThick} & @@@ pointListT

Both give

{{0, -GlassThick + RBase}, {Middle, -GlassThick + RMiddle}, {Height, -GlassThick + RTop}}

$\endgroup$
1
  • $\begingroup$ This is super helpful, thanks! $\endgroup$ Commented Dec 8, 2016 at 20:17

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