2017-05-18 145 views
1

我想将[%a/b]更改为​​3210。 基本上,同为Change path or refinement,但file!代替:更改文件路径

我想块里面a/b改变a/c

test: [a/b] 

在这种情况下,无论是change next test/1 'ctest/1/2: 'c作品。 但不是当testfile!

>> test: [%a/b] 
== [%a/b] 
>> test/1 
== %a/b 
>> test/1/2   ; can't access 2nd value 
== %a/b/2 
>> next first test ; not quite what you expect 
== %/b 

试图change它给不是你所期望的:

>> change next test/1 'c 
== %b 
>> test 
== [%acb] 

回答

4

你很困惑path!file!系列,它们可以看起来很相似,但是它们的性质有很大的不同。

path!是用斜线符号分隔值(通常word!值)的集合,一个file!char!值的集合。 file!系列中的斜杠字符只是字符,因此file!对任何子结构都没有任何了解。它具有(主要)string!系列的语义,而path!具有block!系列的语义。

现在,这个被清除,对test/1/2结果,在file!一系列路径符号具有比上string!不同的行为,它会做一个聪明的串联而不是充当访问的。它被称为智能,因为它将很好地处理左侧和右侧部分中存在的额外斜线字符。例如:

>> file: %/index.html 
== %/index.html 

>> path: %www/ 
== %www/ 

>> path/file 
== %www/file 

>> path/:file 
== %www/index.html 

相同的路径符号规则适用于url!系列太:

>> url: http://red-lang.org 
== http://red-lang.org 

>> url/index.html 
== http://red-lang.org/index.html 

>> file: %/index.html 
== %/index.html 

>> url/:file 
== http://red-lang.org/index.html 

因此,对于改变test: [%a/b]嵌套内容,file!主要表现为string!,你可以使用任何可用的方法字符串来修改它。例如:

>> test: [%a/b] 
== [%a/b] 

>> change skip test/1 2 %c 
== %"" 

>> test 
== [%a/c] 

>> change next find test/1 slash "d" 
== %"" 

>> test 
== [%a/d] 

>> parse test/1 [thru slash change skip "e"] 
== true 

>> test 
== [%a/e] 
+1

'解析测试/ 1 [THRU斜线变化到结束的“e”]'是我需要。谢谢 –

3

文件是字符串类型,并且可以在你想以同样的方式被操纵修改一个字符串。例如:

test: [%a/b] 
replace test/1 %/b %/c 
3

这是因为文件!是一个任意字符串!,而不是任何数组!

>> any-string? %a/c 
== true 
>> any-array? 'a/c 
== true 

因此,目录分隔符'/'在文件中!对于CHANGE行动并不意味着什么特别。因此,%a/b中的'a','/'和'b'将以同样的方式处理,并且解释器不会将其解析为两段文件路径[a b]。

虽然对于路径来说!因为它是一个数组,每个组件都是一个rebol值,解释器知道这一点。例如,/ bcd中的'bcd'将被视为一个整体(一个单词!),而不是三个字符'b','c'和'd'。

我同意该文件!成为任何字符串!不方便。

1

这里是一个可能麻烦的解决方案,但适合的目录处理它们作为文件

test/1: to-file head change skip split-path test/1 1 %c