1

I want to find files in a directory, then split the pathname and print each part of path on a separate line:

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

However it is giving following error:

$ 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)

The error message is really long and complex!

Both findTokens and splitOn are not working.

Where is the problem and how can this be solved.

4

1 回答 1

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
]
于 2019-05-10T14:23:43.290 回答