stringsplitsmalltalkgnu-smalltalk

Why file path string is not splitting


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.


Solution

  • The message maybe long but the line says the reason:

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

    You probably want to use a split differently, probably using subStrings: $character. I just tried it on GNU Smalltalk windows version:

    The command:

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

    The result:

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

    Where you get your path when you have it in the collection. You start either from beginning or end.

    For example you can start from beginning like this:

    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
    ]