【CryptoZombies】lesson1 Chapter 8: Working With Structs and Arrays

struct Person {
 uint age;
 string name;
}

Person[] public people;

Now we're going to learn how to create new Persons and add them to our people array.

// create a New Person:
Person satoshi = Person(172, "Satoshi");

// Add that person to the Array:
people.push(satoshi);

We can also combine these together and do them in one line of code to keep things clean:

people.push(Person(16, "Vitalik"));

一行にすると綺麗にまとまる。
しかし、最初は二行で書いて少しずつ慣れていく方が好ましいかも。


Note that array.push() adds something to the end of the array, so the elements are in the order we added them. See the following example:

uint[] numbers;
numbers.push(5);
numbers.push(10);
numbers.push(15);
// numbers is now equal to [5, 10, 15]

test

Let's make our createZombie function do something!

1.Fill in the function body so it creates a new Zombie, and adds it to the zombies array. The name and dna for the new Zombie should come from the function arguments.
2.Let's do it in one line of code to keep things clean.

pragma solidity >=0.5.0 <0.6.0;

contract ZombieFactory {

   uint dnaDigits = 16;
   uint dnaModulus = 10 ** dnaDigits;

   struct Zombie {
       string name;
       uint dna;
   }

   Zombie[] public zombies;

   function createZombie (string memory _name, uint _dna) public {
       // Zombie zombie= Zombie(_name, _dna)
       // Zombies.push(zombie);
       zombies.push(Zombie(_name, _dna));
   }

}

個人的にはここで躓いた。ゴール(zombies.push(Zombie(_name, _dna));)から逆算すると、理解が進んだ。

// Zombie zombie= Zombie(_name, _dna)
// Zombies.push(zombie);

の過程が大切。


この記事が気に入ったらサポートをしてみませんか?