Home   Archive   Permalink



Parse pattern matching

Continuing my struggle with parsing, I have a folder of files, and I want to do something with some of them but not all of them. The names are like the ones in the sample below. What I want to do is, process those files whose names BEGIN with "CV_Permits" and SKIP all the other files.
    
Since I don't know how to specify that the names must BEGIN WITH "CV_Permits" it would sort of work to SKIP those files that DO start with "Log" however that is not ideal because the folder does contain other files that start with neither "CV_Permits" nor "Log."    
    
If anyone can offer guidance, I will put the solution into my collection of parsing examples so I never will have to ask again.
    
Thank you. I can brute-force a non-parsing solution, but it seems like parsing would be smoother.
    
R E B O L []
    
FILENAMES: [
     %CV_Permits_2-1-2018.txt
     %CV_Permits_2-4-2018.txt
     %Log_CV_Permits_2-1-2018.txt
     %Log_CV_Permits_2-4-2018.txt
]
    
foreach ID FILENAMES [
;; -- Would prefer the reverse logic of deciding which ones to
;; -- KEEP rather than which ones to SKIP.
     either parse to-string ID ["Log_"] [
         print ["Skip " ID]
     ] [
         print ["Process " ID]
     ]
]
    
halt

posted by:   Steven White       22-Oct-2018/15:13:54-7:00



You're looking for some fairly elementary pattern matching here that PARSE can certainly handle, though FIND will also be your friend here:
    
PARSE: remember that for parse to return TRUE, you have to provide a rule that describes the input string:
    
foreach ID FILENAMES [
     if parse ID ["CV_Permits_" to end][ ; to end will match everything after your prefix
         print ["Process:" ID]
     ]
]
    
FIND: the /MATCH refinement will only return 'truthy' if the search string is at the current index:
    
foreach ID FILENAMES [
     if find/match ID "CV_Permits_" [
         print ["Process:" ID]
     ]
]


posted by:   Chris       22-Oct-2018/15:33:36-7:00



Name:


Message:


Type the reverse of this captcha text: "k c e h c - d a e r"



Home