【问题标题】:Why file path string is not splitting为什么文件路径字符串不拆分
【发布时间】:2019-09-28 09:06:15
【问题描述】:

我想在目录中查找文件,然后拆分路径名并将路径的每一部分打印在单独的行上:

(Directory working: '.')
allFilesMatching: '*.st' do: [ :ff | (ff name)
    findTokens: '/'     "Linux separator"
    "splitOn: '/'        -this also does not work"   
    do: [ :i|
        i displayNl ]]

但是它给出了以下错误:

$ gst firstline.st 
"Global garbage collection... done"
Object: '/home/abcd/firstline.st' error: did not understand #findTokens:do:
MessageNotUnderstood(Exception)>>signal (ExcHandling.st:254)
String(Object)>>doesNotUnderstand: #findTokens:do: (SysExcept.st:1448)
optimized [] in UndefinedObject>>executeStatements (firstline.st:3)
[] in Kernel.RecursiveFileWrapper(FilePath)>>filesMatching:do: (FilePath.st:903)
[] in Kernel.RecursiveFileWrapper>>namesDo:prefixLength: (VFS.st:378)
[] in File>>namesDo: (File.st:589)
BlockClosure>>ensure: (BlkClosure.st:268)
File>>namesDo: (File.st:586)
Kernel.RecursiveFileWrapper>>namesDo:prefixLength: (VFS.st:373)
Kernel.RecursiveFileWrapper>>namesDo: (VFS.st:396)
Kernel.RecursiveFileWrapper(FilePath)>>filesMatching:do: (FilePath.st:902)
File(FilePath)>>allFilesMatching:do: (FilePath.st:775)
Directory class>>allFilesMatching:do: (Directory.st:225)
UndefinedObject>>executeStatements (firstline.st:2)

错误信息真的很长很复杂!

findTokenssplitOn 都不起作用。

问题出在哪里,如何解决。

【问题讨论】:

    标签: string split smalltalk gnu-smalltalk


    【解决方案1】:

    消息可能很长,但行说明了原因:

    Object: '/home/abcd/firstline.st' error: did not understand #findTokens:do

    您可能想以不同的方式使用拆分,可能使用subStrings: $character。我刚刚在 GNU Smalltalk windows 版本上尝试过:

    命令:

    'C:\prg_sdk\GNU Smalltalk(x86)\share\smalltalk\unsupported\torture.st' subStrings: $\

    结果:

    OrderedCollection ('C:' 'prg_sdk' 'GNU Smalltalk(x86)' 'share' 'smalltalk' 'unsupported' 'torture.st' )
    

    当您在集合中获得路径时,您会从哪里获得路径。你要么从头开始,要么从头开始。

    例如,您可以像这样从头开始:

    resultPath := nil.
    pathCollection := 'C:\prg_sdk\GNU Smalltalk(x86)\share\smalltalk\unsupported\torture.st' subStrings: $\.
    pathCollection do: [ :eachPartPath |
         resultPath := (resultPath isNil) ifTrue: [
            eachPartPath
        ] ifFalse: [
            resultPath, '\', eachPartPath
        ].
        resultPath displayNl
    ]
    

    【讨论】:

    • 但是为什么 findTokenssplitOn 不起作用?
    • 我缩短了文件路径名以降低复杂性。
    • #findTokens:#splitOn: 可能用于不同的 Smalltalk 实现,如 Pharo,但不在 GNU Smalltalk 中。
    猜你喜欢
    • 2015-02-15
    • 1970-01-01
    • 2012-05-17
    • 2013-06-13
    • 2011-12-29
    • 1970-01-01
    • 1970-01-01
    • 2014-09-04
    • 1970-01-01
    相关资源
    最近更新 更多