5-minute understand YAML
Translation: liubin http://www.ruby-cn.org/ Original: http://yaml.kwiki.org/index.cgi?homepage
Document location: http://www.ruby-cn.org/articles/yaml_in_5_minutes.html
2004/11/11
Translator Note: Before you begin, maybe, you want to see YAML's basic concept, if yes, please click here: http://www.ruby-cn.org/articles/what_is_yaml.html
First minute: a simple list
Your friend will send you a letter:
- Memoirs Found in a bathtub
- Snow crash
- ghost world
At the same time, he also asked you to read these books in the order above, he wants to discuss the contents of these books.
In YAML, these sequential lists are called sequence. The sequence includes a set of sequential data that is not changed when you load this sequence in the program.
In many flexible languages, you can access this sequence and the value in a specified index. The first item of this sequence is expressed in 0.
In PHP, Perl, or Ruby, you can:
Print $ book_list [0];
The print results are as follows:
Memoirs Found in a bathtub
This is also the first item on the list of books above.
Other lists
List (LISTS) can include various data:
---
- 1 # Numbers
- Snow crash # strings
- 2003-07-24 # dates
The above list begins with three "-", these three "-" is a document division (Document Separator), which represents the beginning of a YAML document. Of course, you can also divide multiple documents, such as:
---
- 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
Second Minute: A simple dictionary
When you are immersed in reading Harry Potter, your friends send new lists, this list includes the author's name:
Stanislaw Lem: Memoirs Found in a bathtub
NEAL Stephenson: Snowcrash
Daniel Clows: Ghost World
The above dictionary has no order, but each author corresponds to a book name. Your friend wants you to find the book you want according to the author. (Note no "-" in front
In YAML, such a dictionary is called MAP, and MAP is usually used to store data, and the colon is called Key, the right side is called Value.
KEY is used as an index of data from MAP.
In PHP or Ruby, you can write this:
Print $ books_by_author ['Daniel Clows'];
Will print as follows:
MAP in Ghost WorldSequernCE
Your friend sent you a list of updated, including the author, title, and a certain order, I hope you can read it in this order.
---
- STANISLAW LEM: Memoirs Found in A Bathtub
- NEAL Stephenson: Snowcrash
- Daniel Clows: Ghost World
In the above sequence, each item is a MAP, and when a collection includes another collection, it is called Nesting.
Third minute: something in the mailbox
In the third minute, let's slow down, the teacher let your friend bother you, you think that it is not long after you will be mad.
冉 Let's take a look at your mailbox. You live in a apartment, your mailbox has only one small mouth to be invited, next to hundreds of mailboxes.
You check my own mailbox and discovered five letters. Two is given to your neighbors Jim O'Connor, a company called "Hank Bros .: The Car Wash!", The other two letters are your own.
You intend to complaint with your own mailbox, you plan to send a letter to the post office, let them see how much it has brought to you.
You started a new YAML document, including today's dates, and letters in your mailbox:
---
Date: 2003-07-25
Letters TO:
"Hank Bros .: THE Car Wash!": 1
Jim O'Connor: 2
Myself: 2
In this example, a map nested another map, the first MAP key is, its value is a Nested Map, and the second MAP is indented.
In PHP or Ruby, you can access this:
Print $ Mailbox ['letters to'] ['myself'];
Will print:
2
You also found it, "Hank Bros .: The Car Wash!" Expanded with a double silver number, because this included a good. Otherwise, it will cause trouble, you will not be able to figure out which one is value Value.
Fourth minute: More news
You got some explanations from the post office:
---
Concerning Car Washes:>>
We are sorry to have misplapaced 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 also shows us another feature of YAML: Block. In their information, I used Block.
In YAML, a Block is a piece of text that indent one or more spaces.
Pay attention to this greater than the number:>. It represents the beginning of "Folded Block". The so-called Folded Block, is a row, a line, next to the sentence, will form a paragraph, which can read like we read the text.
The above message will be divided into two rows in YAML readers, and is divided by a carriage return.
Controlling Words
The above news has not finished yet, this is behind:
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 Toldur Postman That You Screen His Mail.
. - h h.
- He Held a raygun to the postmans head.
This looks a bit weird, the person of the post office uses a vertical line, replaced the original greater than the number, is this different?
A vertical line (also known as the pipe PIPE), indicating the beginning of a Literal Block. LITERAL Block means each sentence self-contained section, this block will be a literal value as the computer read.
The YAML reader will treat the above block as a 5 line (the first line is blank).
Fifth minute: Is this all?
To now, it seems that YAML is so much, it's just a collection of some lists and dictionaries?
This is just the most basic thing we can talk about by our five minutes. Before you continue, you will see the last concept.
Here is a record of time to read this five-minute tutorial:
---
Name: Dirk Hockeybranch
Minutes spute:
- 1.02
- 1.34
- 0.7
- 0.89
- 0.94
Above We use a sequence nested in a map, each item in the sequence represents the time used in the first step. If everyone sent yourself like this like this, we need to prepare a more accurate name to this tutorial, such as Yaml in Five-Point-One-Two Minutes. This will be a bit Hua. (We may need a lot of space to write such a document.)
A wireful YAML user may use inline sequence to record the time they used:
---
Name: Dirk Hockeybranch
Minutes sales: [1.02, 1.34, 0.7, 0.89, 0.94]
For short lists, you can use them in a row, and all between commas, and finally enclose them.
INLINE MAPS is also similar:
---
Minutes spnt: {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.
in conclusion
How long does it take? five minutes? Still ten minutes?
If you want to learn more in-depth, I suggest you go to see YAML Cookbook, a very good manual, it will explain Yaml more detailed, you will be amazed to YAML.
Finally, thank you for speaking time to read this article.
Translator Note: The following is some of the resources I found, it may be useful to you
1.YAML hometown: http://yaml.org/
2.XML question: YAML Improvements to XML: http://www-900.ibm.com/developerWorks/cn/xml/x-matters/part23/index.shtml
3. Recently launched YAML specification 1.0, at http://yaml.org/spec/