Parsing query. Even Better example.
[1/6] from: tbrownell:l3technology at: 27-Feb-2003 17:25
str: {Some text... Section 1
abc1, blah blah
=> abc2, blah, blah blah
=> abc3, blah blah, blah
=> abcr, blah, blah
Section 2
abchk, blah blah, blah
=> bdc6, blah blah, blah
=> ghj, blah, blah
}
Where I end up with...
n: [["abc1" "abc2" "abc3" "abcr"] ["abchk" "bdc6" "ghj"]]
Not only do I need to collect all of n, I also need to count the number of
items in each section (3 in first and 2 in second).
Observations...
- the first value of each section follows the "^/" of the line with
Section
in it
- Each subsequent value is the first word following "=>"
TB
[2/6] from: joel:neely:fedex at: 27-Feb-2003 23:07
Hi, Terry,
This is far from the most elegant solution you'll receive, I'm
sure, but I think it's understandable...
Terry Brownell wrote:
> str: {Some text... Section 1
> abc1, blah blah
<<quoted lines omitted: 8>>
> Where I end up with...
> n: [["abc1" "abc2" "abc3" "abcr"] ["abchk" "bdc6" "ghj"]]
Here are your test strings:
8<----------
str1: {Some text... Section 1
abc1, blah blah
=> abc2, blah, blah blah
=> abc3, blah blah, blah
=> abc4, blah, blah
Section 2
abc5, blah blah, blah
=> abc6, blah blah, blah
=> abc7, blah, blah
}
str2: {Some text... Section 1
abc1, blah blah
=> abc2, blah, blah blah
=> abc3, blah blah, blah
=> abcr, blah, blah
Section 2
abchk, blah blah, blah
=> bdc6, blah blah, blah
=> ghj, blah, blah
}
8<----------
and here's a fairly brute-force solutions:
8<----------
terry: make object! [
whitespace: charset "^-^/"
wordchars: charset [#"a" - #"z" #"A" - #"Z" #"0" - #"9"]
results: []
subresults: []
oneword: ""
began: ""
ended: ""
subquery: func [] [
subresults: copy []
parse copy/part began ended [
any whitespace
any [
copy oneword some wordchars
thru "=>" any whitespace
(
append subresults oneword
)
]
copy oneword some wordchars
to end
(
append subresults oneword
)
]
append/only results subresults
]
query: func [s [string!]] [
results: copy []
parse/all s [
to "Section" to newline
began:
any [
skip to "Section" ended: (subquery)
to newline
began:
]
to end ended: (subquery)
]
results
]
]
8<----------
The strategy is probably obvious; break up into sections,
processing each section individually. I was trying to keep
the logic at each level as simple as possible, comparable to
your verbal description:
- The string is made up of sections, each beginning with the
word "Section", and some other text up to the newline.
- Within a section, words are accumulated into a block that
is nested inside the main result.
- The first word begins after the initial whitespace (newline
after the "Section" line);
- Subsequent words follow consecutive "=>" markers.
I'm eager to see how someone else may optimize this or offer
a better solution.
-jn-
[3/6] from: rebol:laurent-chevalier at: 28-Feb-2003 12:57
Here is a solution in one long line :
>> n: copy [] first: n parse str [ any [ "=> " copy data to "," (append
n data) | "Section" thru newline copy data to "," (append/only first
compose[(data)] n: last first) | skip ] ] first
== [["abc1" "abc2" "abc3" "abcr"] ["abchk" "bdc6" "ghj"]]
Laurent
Terry Brownell wrote:
[4/6] from: joel:neely:fedex at: 28-Feb-2003 6:32
Hi, Terry,
How rigid/consistent is your syntax? Could something like
str: {Some text... Section 1
abc1 blah blah
=> abc3; blah blah blah
=> def
=> ghi, jkl
Section 2
jkl: fee fie foe fum
fiddledeedee
=> qwert, yuiop
}
possibly occur?
-jn-
Terry Brownell wrote:
[5/6] from: rebol:laurent-chevalier at: 28-Feb-2003 18:03
Well, the same solution but without using the word 'first unlike my
previous post.
n: copy []
result: n
parse str [
any [
"=> " copy data to "," (append n data)
| "Section" thru newline copy data to "," (
append/only result compose[(data)]
n: last result
)
| skip
]
]
probe result
Laurent
http://www.shlik.org
Terry Brownell wrote:
[6/6] from: tbrownell::l3technology::com at: 28-Feb-2003 9:40
Hi Joel,
The only things that can change are the actual values I'm looking for, and
the number of Senses. The fact that the first one isn't after a "=>" and
the rest subsequently are, remains the same.
TB
Notes
- Quoted lines have been omitted from some messages.
View the message alone to see the lines that have been omitted