I have searched and read through countless questions & answers here, scoured the interwebs, etc. and I cannot find out whether what I want to do is even doable, much less how. I don't know if I am searching for the wrong terms, looking past the answer, or just being stupid. (FYI: After being a developer on a different large on-premise CRM for almost 5 years, I am only approximately 8 months new to Salesforce; so go easy, please, if I am being stupid.)
I have found countless ways to get the value of an sObjectField using a variable. E.g.,
Account a = new Account(Name = 'Test Account'); String n = 'Name'; String result = a.get(n); // result == 'Test Account'
This can obviously be "genericized" using objects, lists, maps and whatnot; and then with
for loops or other iteration, one could get multiple field values when the field names were not previously known.
Can you do something similar to set a field value using a variable for the field name?
What I've Tried
a.n = 'New Account Name'; // Results in "Variable does not exist: n" error.
a.set(n) = 'New Account Name'; a.get(n) = 'New Account Name'; a.(a.get(n)) = 'New Account Name'; (sObjectField) a.n = 'New Account Name'; (sObjectField) a.get(n) = 'New Account Name'; // All result in "Expression cannot be assigned" error.
Schema.SObjectType.Account.fields.getMap() and tried all sorts of permutations and combinations of things. (I didn't record all that I tried because I tried hacking at it over and over and can't remember everything.)
I've individually tried...
sObjectField n = Account.Name; Account.Name.getDescribe(); Account.Name.getDescribe().getName(); Account.Name.getDescribe().getsObjectField();
... all followed by
a.n = 'New Account Name';
(which honestly wouldn't really help since the
Name part of those would also need to be a variable for it work in a real use case).
And all sorts of things with
 that obviously didn't work.
In a test data factory class I was wanting to iterate a map that has field names and values (where the field names could be different every time) and then create an object with those field names and values. If I can't use a variable to set a field name, I'm going to have to go through and check every possible field name. That obviously makes for MUCH more code; and if we ever add new fields to that object and would want to be able to set those in a test, we would have to add those fields to this code.