Get to know yaml in 5 minutes

Source: Internet
Author: User

Get to know yaml in 5 minutes

Http:// Homepage

Document location:Http://


Note: before you start, you may want to see the basic concepts of yaml first. If yes, click here:Http://

First minute: a simple list

Your friend sent you the following letter:

- Memoirs Found in a Bathtub- Snow Crash- Ghost World

At the same time, he also asked you to read these books in the above order. He wants to discuss the contents of these books with you.

In yaml, these ordered lists are calledSequence(Sequence). A sequence includes a set of ordered data. When you load the sequence in a program, the sequence remains unchanged.

In many flexible languages, you can use the specified index to access the sequence and its values. The first entry of the sequence starting with 0.

In PHP, Perl, or Ruby, you can:

print $book_list[0];

The output is as follows:

Memoirs Found in a Bathtub

This is also the first item in the book list above.

Other list

A list (lists) can contain various types of data:

---- 1           # Numbers- Snow Crash  # Strings- 2003-07-24  # Dates

The above list starts with three "-". The three "-" areDocument delimiter(Document Separator), Which indicates the beginning of a yaml document. Of course, you can also divide multiple documents, such:

---- Harry Potter and the Prisoner of Azkaban- Harry Potter and the Goblet of Fire- Harry Potter and the Order of the Phoenix---- Memoirs Found in a Bathtub- Snow Crash- Ghost World
Minute 2: A simple dictionary

When you are immersed in reading Harry Potter, your friend sent a new list, which includes the author's name:

Stanislaw Lem: Memoirs Found in a BathtubNeal Stephenson: SnowcrashDaniel Clowes: Ghost World

The dictionary above has no order, but each author corresponds to the name of a book. Your friends hope that you can find the book you want Based on the author. (Note that there is no "-" above "-")

In yaml, such a dictionary structure is calledMapMAP is usually used to store paired data.KeyThe name on the right isValue.

KeyUsed as an index to retrieve data from a map.

In PHP or Ruby, you can write as follows:

print $books_by_author['Daniel Clowes'];

The following content is printed:

Ghost World
Map in sequence

Your friend sent you an updated list, including the author, title, and order. I hope you can read it in this order.

---- Stanislaw Lem: Memoirs Found in a Bathtub- Neal Stephenson: Snowcrash- Daniel Clowes: Ghost World

In the above sequence, each item is a map. When a collection includes another set, it is calledNesting(Nesting).

Third minute: emails

In the third minute, let's take a look at the speed. The teacher asked your friends to disturb you so much that I thought you would be mad soon.

Let's look at your mailbox. You live in an apartment, and there is only one mouth in your mailbox to send messages. There are hundreds of other mailboxes next to it.

Check your mailbox and find five emails. The two messages are for your neighbor Jim o'connor, and the other one is for "thank Bros.: The car wash! "Company, the other two emails are yours.

You are going to complain about the extra emails in your mailbox. You are going to send a letter to the post office to show them how much trouble it has caused you.

You started a new yaml file, which contains today's date and letters in your mailbox:

---date: 2003-07-25letters to:  "Hank Bros.: the Car Wash!": 1  Jim O'Connor: 2  Myself: 2

In this example, a map is nested with another map. The key of the first map is, and its value isLettersA nested map. The second map is indented.

In PHP or Ruby, you can access:

print $mailbox['letters to']['Myself'];

Will print:


You also found out, "Hank Bros.: The car wash! "I expanded it with the dual-silver account, because it was okay. Otherwise, it may cause trouble. You may not know which is the key and which is the value.

Fourth Minute: more messages

You get some explanations from the post office:

---Concerning Car Washes: >  We are sorry to have misplaced this letter.  We were told by a reliable source that you  were the owner of "Hank Bros.: the Car Wash!".  Sorry.

The post office is incorrect, but this shows us another feature of yaml: block (Block). Block is used in their information.

In yamlBlockIt is a piece of text with one or more spaces indented.

Note the following:>. It indicates a"FoldedBlock. The so-called folded block is a line of sentences next to each other to form a paragraph, which can be read as we read the text.

The above message is divided into two lines by a carriage return and line feed in yaml reader.

Controlling words

The above message is not complete yet. This is the following:

Concerning "Jim O'Connor": |  You are receiving Jim O'Connor's mail for several reasons:  - The nameplate on your mailbox still says his name.  - He has told our postman that you screen his mail.  - He is living in your ceiling.  - He held a raygun to the postmans head.

This looks a bit odd. The post office people use a vertical line to replace the original Big Yu. What is the difference?

A vertical line (also called a pipe)Pipe) IndicatesLiteralBlock. The literal block indicates that each sentence is a self-contained part. This block will be a nominal value as it is read by a computer.

The yaml reader treats the above block as 5 rows (the first row is empty ).

Minute 5: Is that all?

It seems that yaml is just a collection of lists and dictionaries?

This is just the most basic thing we can talk about in five minutes. Before proceeding, let's look at the last concept.

Here is a record of the time used by a person to read this five-minute Tutorial:

---name: Dirk Hockeybranchminutes spent:  - 1.02 - 1.34 - 0.7 - 0.89 - 0.94

We nested a sequence in a map above. Each item in the sequence represents the time used in the first step. If everyone sends the time they read to us like this, we need to prepare a more accurate name for this tutorial, suchYaml in five-point-one-two minutes. This will be a bit flashy. (We may need a lot of space to write such a document .)

A clever yaml user may useInline SequenceTo record their time:

---name: Dirk Hockeybranchminutes spent: [1.02, 1.34, 0.7, 0.89, 0.94]

For short lists, you can place them all in one row, separate them by commas, and enclose them in brackets.

Inline mapsIt is also similar:

---minutes spent: {one: 1.02, two: 1.34, three: 0.7,                four: 0.89, five: 0.94}

The above should also be used with caution. Block cannot be used in inline sequences or Inline maps.


How long has it been? Five minutes? Or ten minutes?

If you want to learn more about yaml, I suggest you read the yaml cookbook, a very good manual, which will explain yaml in more detail, you will be amazed at the functions provided by yaml.

Finally, thank you for taking the time to read this article.


Note: The following are some resources I have found that may be useful to you.

1. yaml home:

2. xml problem: Improvement of yaml on XML:

3. Recently introduced yaml Specification 1.0 in



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.