How to specify ranges in YAML?
Asked Answered
W

4

14

I can express

3rd page is the title page

in YAML

title: 3

What about the following?

Pages 10 to 15 contains chapter 1

One way is

chapter 1: [10, 11, 12, 13, 14, 15]

I would prefer a range here. Is there anything like that in YAML?

chapter 1: (10..15)

** Update **

The following would be my alternative if there is no such thing as range in YAML

chapter 1:
   start page: 10
   end page: 15
Whinny answered 26/7, 2010 at 16:52 Comment(2)
Why not just chapter 1: [10,15]? Then create a function in Ruby that converts the list to a range?Kindly
Too bad they don't support things like key: [3,6,8-15,18-25]. It's really inconvenient to list all the numbers.Innerve
K
18

There is not direct way to specify ranges in YAML, but some YAML can store serialized objects, for example in Ruby:

...
normal range: !ruby/range 10..20 
exclusive range: !ruby/range 11...20 
negative range: !ruby/range -1..-5 
...

Look here

Kinlaw answered 26/7, 2010 at 17:4 Comment(3)
Thx. But that's not good for me. I am surprised how such a simple thing is not thought about in YAML spec. Or may be all rubyists think range is such a basic thing! :-)Whinny
And what language are you using?Kinlaw
I am using Ruby. But I would prefer to have this yaml file readable editable for the user. !ruby/range will scare the user.Whinny
M
6

Range is application specific. The following may be meaningful for some applications:

-1 .. Q

a .. Щ

23 .. -23.45

1 .. 12:01:14 (both are integers in YAML !)

But the ruby way is also unclear since it does not say whether the end values are included or not: 10 .. 15

(Are you only talking about ranges of integers ?)

Mestee answered 27/7, 2010 at 10:25 Comment(1)
To your question, even integer ranges would do for me. Ruby range is not unclear. It has both inclusive exclusive range syntax. (1..5) inclusive; (1...5) exclusiveWhinny
G
2

Andrey is right - there is no such thing as a basic range. Ranges can be defined on top of totally ordered data types. YAML does not even know the concept of ordering so it makes no sense to talk about ranges in YAML. YAML only knows the concept of node types, the concept of equality, and some predefined kinds of links between nodes. By the way I don't know any other data serialization lange (JSON, XML, CSV, Hessian, Protocol Buffers...) that natively supports ranges.

Garton answered 6/8, 2010 at 11:25 Comment(0)
L
-1

Actually, for ranges like

What about the following?

Pages 10 to 15 contains chapter 1

One way is

chapter 1: [10, 11, 12, 13, 14, 15]

is possible to use arrays (element [0] or even elements - start page, element [1] or odd elements - end page):

chapter1: [10, 15]

chapter2: [16, 20, 22, 40]

if chapter2 has break on page 21

Leapfrog answered 22/6 at 21:8 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.