Solidity Basics

@jon_m
Many thanks for your explanation. You explained it way better than other sources I have been reading. I am a beginner in terms of mapping. I hope you don’t mind getting back to you in the future should I get stuck with anything.
Sincerely,

1 Like

Hi,
I followed Fillip’s mapping video till the end. I created the code, compiled it (no errors) and deployed it. I still don’t know how to use this way to do mapping though:

people.push (Person (people.length, name, height, age))

He showed it in the previous video before mapping, but did not explain how to continue using it since it looks easier.

1 Like

Filip, I believe that there is a little mistake at the beginning of the video where you describe an unsigned integer (of type uint) as one that can be both positive and negative. It seems to me that “unsigned” means that there is no sign. So unsigned integers, by definition, are greater than or equal to zero. To confirm this, I tried assigning a negative value to a uint variable, and, as expected, the compiler produced a corresponding error message.

1 Like

@filip

Hey, can you tell me why I keep getting a compiling error on the code that I am doing from video 4 on struct please? I have attached a screen shot :slight_smile:

Hi @TommyBrook,

I’m glad that explanation helped. You may have noticed that I removed my original reply. That’s because I needed to correct it for one inaccuracy that I realised afterwards. So I’m reposting a new improved version, here, now (see below). Basically, I said mappings don’t store the actual values, but actually they do — they just store them in a different way to arrays. Everything else about the explanation and analogy is broadly correct, at least to get a basic understanding of the differences between mappings and arrays. Please, be aware that this explanaation is oversimplified, but that’s often what we need when we first try to understand these concepts. Then it becomes a journey of discovery, where you will gradually realise the finer details for yourself, over time. At least this is a start…

The other thing I’m updating the explanation for, is the fact that arrays store their values based on a sequence of indices. So the key/value pairs are effectively index/value pairs. With mappings, there is no order to the values, just key/value pairs (e.g. address/value), so you need to provide the specific key that is mapped to the value, in order to access it.

Anyway, here’s the updated explanation:

Think of an array as being like a filing system/cabinet. You can search around in it, retrieve data from specific places in it, and you can iterate over it. All of the values stored in an array are like the contents/data in each of the files in a filing cabinet. These values (file contents) are ordered according to a sequence of indices (0, 1, 2, 3 etc.), a bit like filing reference numbers. Each index (file reference number) and it’s associated value (file contents/data) is a key/value pair (the key being the index).

A mapping is more like a telephone operator (bear with me…). If you give it a key (whatever the key has been defined as — let’s say a telephone number for this analogy, but in Solidity it’s more likely to be an address)… with that key (telephone number) the mapping will supply you with the value that the key is mapped to (the telephone operator will connect you to the person who is “mapped” to the telephone number you gave them).

The telephone operator (mapping) can only connect you to the other person (return the value) if you already have their telephone number (key/address). With a filing cabinet (array) you can hunt around (manipulate it / iterate over it etc.) until you find what you’re looking for, because of its ordered sequence of reference numbers (indices). Generally speaking, you can’t iterate over a mapping due to it’s non-sequential nature. However, if you have the key to a value (the telephone number of the person you want to speak to and interact with) the mapping (telephone operator) can “put you through” straight away. If you have very large mappings this will save you a noticeable amount of gas (as opposed to using an array). The advantage of an array is its flexibility, whereas a mapping will give you more privacy (you don’t have to speak to a load of other people until you find the one you actually want to talk to).

Wow! That’s a lot longer than the first draft! But it’s more accurate and will hopefully be of help to other Solidity beginners too. I hope, as well, that’s it’s enhanced your understanding, and not caused a slip back into confusion.

Let me know what you think.

I’m going to look at, and answer, your other question now. Thanks for your patience and for bearing with me.

Not at all! That’s what we’re here for :slightly_smiling_face:

1 Like

Hi @TommyBrook,

I think this will answer your question:

struct Person {
   string name;
   uint age;
   uint height;
}

mapping(address => Person) private people;

/* Filip's version of the createPerson function, in the Mappings video
  (based on Alternative 2 in the previous video, when using an array) */
function createPerson(string memory name, uint age, uint height) public {
   address creator = msg.sender;   

   Person memory newPerson;
   newPerson.name = name;
   newPerson.age = age;
   newPerson.height = height;

   people[creator] = newPerson;
}
/* Alternative version of createPerson function, not shown in the Mappings video
   (based on Alternative 1 in the previous video, when using an array) */
function createPerson(string memory name, uint age, uint height) public {
   address creator = msg.sender;
   people[creator] = Person(name, age, height);
}
1 Like

Hi Frank,

Yes, you are correct :+1:

As you say…

uint positiveNumber = 5;

uint negativeNumber = -5;      // => compiler throws an error

(i) uint only allows zero and positive integers up to a max based on the binary places included in the type.
    e.g. uint4 (0 to 15) , uint8 (0 to 255) , uint16 (0 to 65535)

(ii) int allows positive and negative integers from a min of minus(-) half the max based on the binary places included in the type, to a max of plus(+) half the max based on the binary places, and including zero.
    e.g. int4 (-8 to 7) , int8 (-128 to 127) , int16 (-32768 to 32767)

We should always keep within the restrictions in terms of the range of integers available with a given integer type (signed or unsigned), because the compiler will not always prevent an overflow (a loop from the max limit, back to the min limit) or an underflow (a loop from the min limit back to the max limit).

1 Like

Hello sir, after the people.push, look at the last parameter (“height”), you should not have a comma at the end of the last parameter.

If you have any doubt, please let us know so we can help you!

Carlos Z.

1 Like

Thank you for your reply and I have amendent the comma error, however, I am still getting an error about not recognising ‘lenght’ in the directory, see attached. Again thanks for takin the time to help me out…

AJ

@filip @thecil can anyone help with this error I get now please, tried loads of different things and I might be missing something?!

Cheers

It’s only the variable “people.lenght”, LENGTH, you misspell the property name.

If you have any doubt, please let us know so we can help you! :slight_smile:

Carlos Z.

1 Like

@thecil Thank you sir and lol in me not being able to see that

2 Likes

Don’t worry @coletto27, you’re not the only one. It’s a very common mistake :wink:

1 Like

Hi, I have a question regarding the struct lesson.

My code works but it pushes the first created person under position " 1" in the array, instead of “0” which happened when Filip run his code. I don’t understand why it does this.
Here’s my code:

pragma solidity 0.5.12;

contract HelloWorld{

struct Person{
    uint id;
    string name;
    uint age;
    uint height;
   
}
Person[] public people;

function createPerson(string memory name, uint age, uint height) public{
    people.push(Person(people.length, name, age, height));
}

}

Hello, seeking assistance with deploying on this version of remix. Please see screenshot. I think it is adding plugins? Should I activate them all? Also trying to find how to compile as well, is that the bugs? Or where to choose the compiler as it isn’t clear…will play around a bit.

Salud.

1 Like

Hi,

Via Plugin Manager, you can install DEPLOY & RUN TRANSACTIONS and SOLIDITY COMPILER plugins. This last plugin will find the right compiler to match the pragma statement.

2 Likes

https://remix.ethereum.org
I’m not getting anything. It goes out and finds it but nothing is there.

https://remix.ethereum.org does not give me anything. No error, just blank page.

I had the same thing when using the Brave browser…still am using Brave, just shut off the brave shields and waited and it takes time to load.

2 Likes

that’s great…however I got a bit click happy and installed so many that if filled up the left column and there does not appear to be a way to unplug them nor scroll down to see the installed plugs below the settings…

ok,resolved…got to ‘featured plugins’ where I could deactivate…then found the solidity compiler

1 Like