Tuesday 29 January 2013

NEW-LINE function

Ok, I always forget of this essential function, so I write this post hoping to remember it next time I work with blocks.
Did you aver noticed that block can be saved with invisible break lines? Look this example:

>> a: [1 2 3]
== [1 2 3]
>> b: [
1
2
3
]
== [1
2
3
]
>> a = b
== true


Did you notice it? a and b are the same block, but b is saved with "invisible" new lines char.
Computer ignore the "invisible" newline char, but human like you and me need them to understood data in blocks, look another example:

>> address-book: [
Michelle
["+3906500000" "S. Mary St." "Rome" "Italy"]
Carl
["+155500000" "Sassenrath Ranch" "San Francisco" "USA"]
]
>> ? address-book
ADDRESS-BOOK is a block of value: [
Michelle
["+3906500000" "S. Mary St." "Rome" "Italy"]
Carl
["+155500000" "Sassenrath Ranch" "San Francisco" "USA"]
]

It's easy to understand how is made, now try the same blocks without new-lines:

>> new-line/all address-book false
>> ? address-book
ADDRESS-BOOK is a block of value: [Michelle ["+3906500000" "S. Mary St." "Rome" "Italy"] Carl ["+15550000
0" "Sassenrath Ranch" "San Francisco" "USA"]
]


Now, do you understand the importance of "invisible" new-lines in block? They make files human readable!
Rebol has the new-line function to switch or insert new lines in blocks, let's see how it works.
First of all new-line has a refinement called /all that add or remove all newlines from block, setting the newline value true or false, example:

new-line/all address-book false
? address-book
new-line/all address-book true
? address-book


Moreover you can set the new-line on o off also only in a specific point (use at:

new-line at address-book 2 off
>> ? address-book
ADDRESS-BOOK is a block of value: [
Michelle ["+3906500000" "S. Mary St." "Rome" "Italy"]
Carl
["+155500000" "Sassenrath Ranch" "San Francisco" "USA"]
]


I remember you also that ON, TRUE and YES are the same thing on rebol, and OFF, FALSE, NO are the same thing on rebol.

1 comment:

  1. Thank to Semseddin Moldibi for the follwong suggestion:
    /skip is also a useful refinement for new-line (skip every N):

    >> new-line/all/skip [1 2 3 4 5 6] yes 2
    [
    1 2
    3 4
    5 6
    ]

    ReplyDelete