Why doesn't the name get added? #125
-
I'm referring to around the end of the first Remix lesson with the mapping key. I understand that is causes every value you entered in the script to turn to null (or 0), but after I deploy and start adding new names/number (new people), nothing shows up after I click nameToFavorite. Here is my script; I'm not sure what about it is different. Mind all the extra notes: //SPDX-License-Identifier: MIT
pragma solidity 0.8.8; // 0.8.12
contract SimpleStorage {
// types: boolean, uint, int, address, bytes
//bool hasFavoriteNumber = true;
uint256 favoriteNumber;
//People public person = People({favoriteNumber: 4, name: "Russ"});
mapping(string => uint256) public nameToFavoriteNumber;
struct People {
uint256 favoriteNumber;
string name;
}
//uint256[] public favoriteNumbersList;
People[] public people;
// 0: 2, Patrick
//string favoriteNumberInText = "San-Kyu";
//int256 favoriteInt = -39;
//address myAddress = 0xdd4a4C645d01b196B5c8e3ef1902Be957a6a938d;
//bytes32 favoriteBytes = "dog"; // typically like 0x________ (32 is max bits a bytes can be)
function store(uint256 _favoriteNumber) public {
favoriteNumber = _favoriteNumber;
//uint256 testVar = 5;
// favoriteNumber = favoriteNumber +1;
}
// view, pure don't spend gas!
function retrieve() public view returns(uint256){
return favoriteNumber;
}
// calldata, memory and storage
function addPerson(string memory _name, uint256 _favoriteNumber) public {
people.push(People(_favoriteNumber, _name));
nameToFavoriteNumber[_name] = favoriteNumber;
}
}
// 0xd9145CCE52D386f254917e481eB44e9943F39138
// _ is just to make it a different variable |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 1 reply
-
Specifically I want it so that when I click nameToFavorite it will show the name and number of the person and their number I added in the addPerson button. |
Beta Was this translation helpful? Give feedback.
-
The way mapping works is that you store "values" against "keys". In this case, the "key" is the name and the "value" is the favorite number. To perform a lookup (read) operation on a map, you have to pass a "key" which in our case is the name and the map will return a "value" which in our case is the favorite number. We can only retrieve one value based on the key, as keys are unique. If you wish to retrieve all the entries in a map, you will have to maintain an array of keys separately and loop over it from 0 to n and perform a lookup operation on the map with each name as the key, in the code. For now, you will see a textbox against the "nameToFavorite " button, just enter the "key" which would be the name you stored while storing the (name, favorite) number pair, and then click on the "nameToFavorite" button, it should return a value, your favorite number. |
Beta Was this translation helpful? Give feedback.
The way mapping works is that you store "values" against "keys". In this case, the "key" is the name and the "value" is the favorite number.
To perform a lookup (read) operation on a map, you have to pass a "key" which in our case is the name and the map will return a "value" which in our case is the favorite number.
We can only retrieve one value based on the key, as keys are unique.
If you wish to retrieve all the entries in a map, you will have to maintain an array of keys separately and loop over it from 0 to n and perform a lookup operation on the map with each name as the key, in the code.
For now, you will see a textbox against the "nameToFavorite " button, just enter the "key" whi…