How to find and replace in list

Hi, I need help with finding an item in a list and replacing it with another item.. For example, when a player would like to change his username, the system will have to grab his username from the USERNAME list and replace it with ANSWER(new username).

Oh btw.. I have 2 different username variables, one "username" which is for user to login with, and the other one is "usernames" which the list of usernames

1:

2:

I've tried solution 1 but unfortunately it's not working.. Nothing happened when I run it

I think this is exactly what you're looking for. It finds the first instance of the username, and replaces it with the new username.

Now, this is case insensitive, so it's find something like this
1000346375

But the good news is that snap has a setting you can turn on to make case sensitive checks.

If you don't want to turn on the setting, in fear of other scripts failing, then you can go with this method instead.

Since users may want to change their names, you may consider employing a non-mutable user_id as main key for a user’s records.

The user_name would then be in a table relating user_id-s with user_name-s (BTW you may even keep former names of the same user, if you deem such useful).

Changing a user_name would then be done as follows:

Caveat: this “dictionary” approach (using item 1 of each record as key to access its contents will work only if user_id is a text, not a number.

You may wonder how to obtain the user_id from the user_name:

This, again, will only work if user_name is a text.

Unless you use the assoc block from the list utilities library. I prefer to use that whenever I'm dealing with dictionaries that may have number keys.

I agree, if one hasn’t taken steps preventing keys from being numericnote, use of assoc is to be preferred. The latter is much slower at runtime than (the “dictionary” application of) item of though, which may become an issue when a larger database is involved. Besides, using assoc is slightly less convenient. I recently wrote a simple block (lookup) implementing item of for dictionary applications and supporting numeric keys.

Note: a simple measure is adding a non-numeric prefix (such as: “$”) to each and every key.

Supposing you have the bad taste to make usernames case sensitive! Allowing foo and Foo to be two different people is just asking for identity theft.

To be honest, I didn't think too much about that bit. At least the first method I showed was case insensitive (providing that case sensitivity is turned off).

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.