Steven White — 22-Oct-2018/15:13:54-7:00
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
Chris — 22-Oct-2018/15:33:36-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]
]
]
Stone Johnson — 20-Aug-2019/23:23:34-7:00
;; Here is how I do it.
;; First, I write out what needs to be done in p-code.
;; And then I try code at the console
;; For every file, i.e., for all
;; REBOL should try to match those beginning with CV
;; -while ignore the rest of the filename crud
;; REBOL should add those that have matched to a block
names: copy []
forall filenames [
-parse filenames/1 [ "CV" skip to end (
---append names filenames/1
--)
-]
]
;; non-parse way
names: copy []
forall filenames [
-if equal? 1 index? find filenames/1 %CV [append names filenames/1]
]
Why?
Hint: We're asking REBOL to append to a block! only those finds that have a "head view," i.e., have an index position of one
See?
views: copy []
forall filenames [
-if i: index? find filenames/1 %CV [
--append views join "view of find starts at: " i
-]
]
new-line/all views on
probe views