[OpenSCAD] Digging into search( )

runsun runsun at gmail.com
Sat Apr 18 22:39:18 EDT 2015

Spent some time digging into the built-in  search()
:  search( /*match_value* , string_or_vector [, num_returns_per_match [,
index_col_num ] ]/ )*Conclusion first:* complicated, buggy, unpredictable,
giving out unnecessary warnings. Take notes for 2 points on its design:1. 
match_value is set to:        1.1. Can be a single value or vector of
values.    1.2. Strings are treated as vectors-of-characters to iterate
over;    1.3. If match_value is a vector of strings, search will look for
exact string matches.    In practical, match_value can be: list, number,
string (treated as a collection of characters)2. The *return should be
either a list*, when:/ num_returns_per_match/ is unset or set to 1:     
search( "a","abcdabcd" )= [0]      search( "abc","abcdabcd" )= [0, 1, 2]   
*or a list of lists*, when:/ num_returns_per_match/ is set to anything other
than 1 :     search( "a","abcdabcd",0 )= [[0, 4]]     search(
"abc","abcdabcd",0 )= [[0, 4], [1, 5], [2, 6]]      data4= [["a", 1], ["b",
2], ["c", 3], ["a", 4], ["b", 5]]     search( "abc",data4,2 )= [[0, 3], [1,
4], [2]]Observations:A. Since a match_value is treated as a list of chars,
the following 2 should give same results:    search( "abc","abcdabcd" )= [0,
1, 2]    search( ["a","b","c"],"abcdabcd" ) want: [0, 1, 2] got: [[], [],
[]]B. The following searches give same return. Users have no way to know
what are matched:    search( "bc","abcdabcd" )= [1, 2]    search(
"xbc","abcdabcd" )= [1, 2]     search( "xbzjck","abcdabcd" )= [1, 2]C. Users
can't possibly predict the following return:    data9= [ ["cat", 1], ["b",
2], ["c", 3], ["dog", 4]                , ["a", 5], ["b", 6], ["c", 7],
["d", 8]                , ["e", 9], ["apple", 10], ["a", 11] ]     q= ["b",
"zzz", "a", "c", "apple", "dog"]    search( "cat",data9 ) want: [2,4] got:
[0, 4]        This also gives out a warning:  WARNING: search term not
found: "t"D. This is unpredictable, too:     a1=
[["ab",1],["bc",2],["cd",3]]      search( "ab", a1) want: [ ] got: [0, 1]E.
This gives correct answer, but showing two warnings:        WARNING: search
term not found: "p"     WARNING: search term not found: "q"     search(
"pq",a1 )= [ ]F. Inconsistent return when match not found:    search(
"e","abcdabcd" )= []     search( ["zzz"],data9 )= [[]]     Since [[]] is
treated as true, the following would be impossible:    search(...) ?
do_found : do_not_foundSo what I think so far are:1) It is very difficult to
understand how it works. 2) It is still buggy.3) Would make a lot of effort
for users to understand it, and a lot of effort trying to debug, if
possible.  My conclusion is that search() is still buggy and it would
probably not a good idea for it in the release yet. What I believe is that
it tries to accommodate too many usages in a single. For example,
/match_value/ could have been designed as just a value (one of number,
string or list), but not list of values. Since we have list comprehension,
it'd be extremely easy to achieve this:   [ for (m in match_values) search(
m, ...) ]This will take away a large chunk of complication inside the coding
of search().  


$  Runsun Pan, PhD 

$ -- OpenScad_DocTest: doc and unit test ( Github , Thingiverse  ) 

$ -- hash parameter model: here , here 

$ -- Linux Mint 17.1 Rebecca x64  + OpenSCAD 2015.03.15/2015.04.01.nightly 

View this message in context: http://forum.openscad.org/Digging-into-search-tp12421.html
Sent from the OpenSCAD mailing list archive at Nabble.com.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.openscad.org/pipermail/discuss_lists.openscad.org/attachments/20150418/3963a725/attachment-0002.html>

More information about the Discuss mailing list