World: r3wp
[Core] Discuss core issues
older newer | first last |
Ingo 25-Jul-2005 [1563x4] | Q1: I want to replace all 'none in a block with _different_ empty strings, what's the fastest way to do this? (replace/all BLOCK none "" replaces all 'none with the same empty string) |
Q2: I have to blocks containing strings, and want to find out which of these strings differ (I need all differing positions), what do you think is the fastest way to achieve this? | |
Thanks in advance for all ideas! | |
PS. Do you, like me, feel that the replace way of doing things is questionable? | |
Volker 25-Jul-2005 [1567] | b: ["s1" "s2" "s3" "s1"] parse b[any[to "s1" p: (p/1: copy "t0") skip]] ? b |
Ingo 25-Jul-2005 [1568] | Q2 again: Sorry, my axplanaition was a little unclear: I have a row from a database, and store away one block, and display the other for the user. The user may, or may not, change the data. >> orig: ["Mr" "Petr" "Ustinov"] >> data: ["Sir" "Peter" "Ustinov"] >> magic-changed-func orig data == [1 2] |
Volker 25-Jul-2005 [1569] | orig: ["Mr" "Petr" "Ustinov"] data: ["Sir" "Peter" "Ustinov"] ; same length ! out: copy[] repeat i length? orig [ if orig/:i <> data/:i [ append out i ] ] ? out |
BrianH 25-Jul-2005 [1570x3] | Q1:use [x] [ |
Sorry | |
Q1: use [x] [ x: block while [x: find x none!] [change x copy ""] ] | |
Anton 26-Jul-2005 [1573x2] | Ingo, I have similar feelings too sometimes. I need a higher level function to do some things like that, but it's not there.... |
I often wanted reduce and compose to not make a copy for you, but work directly on the block. (Perhaps new functions "induce" and "impose" ?) | |
Sunanda 26-Jul-2005 [1575] | induce should be fairly easy -- walk the block with 'for and 'poke back the value |
Anton 26-Jul-2005 [1576x3] | blk: [a b c] format: [(1 + 2) (random 100) c] impose: func [blk format][repeat n length? format [if paren? format/:n [poke blk n do format/:n]]] impose blk format ;blk == [3 95 c] |
For induce I think I would use do/next. | |
induce: func [blk format][change blk reduce format] ; (except, as a native!, without creating a temporary block) induce blk [1 + 2 random 100 'c] ; == [3 67 c] | |
Ingo 26-Jul-2005 [1579x2] | Hi Voker, BrianH, thanks for your ideas ... they look mighty long compared to normal Rebol code ;-) |
Sorry, I wanted to type Volker, of course! | |
Brett 26-Jul-2005 [1581x3] | ; Q1: copy/deep will give you new strings: block: [none none none] new-block: copy/deep replace/all block 'none {} |
; Q1 Or if you know you can reduce it you could do something obtuse like: block: ["a" none "b" none none] use [none][none: does [copy {}] bind block 'none] reduce block | |
But if you do need to do an actual replace on the original block - see the other solutions :-) | |
Ingo 27-Jul-2005 [1584] | Hi Brett, your first version looks pretty good ... now I'll have to dig out my little profiler, and see what's the fastest of all of these ;-) |
Anton 27-Jul-2005 [1585x4] | I reckon the parse will win. |
Brett's is the nicest looking though. | |
Brett, your obtuse way will only work if you are replacing 'none (as a word), but not none! values. | |
eg. reduce your input block: block: reduce ["a" none "b" none none] | |
Brett 27-Jul-2005 [1589] | Agreed Anton and perhaps that shows the obtuse method is not a great idea for this situation - though perhaps an interesting one ;-) |
Gabriele 29-Jul-2005 [1590] | In case anyone is interested... http://www.rebol.org/cgi-bin/cgiwrap/rebol/view-script.r?script=reboldiff.r |
Henrik 30-Jul-2005 [1591] | Are there any other instances where the a: copy [] vs. a: [] "problem" appears? I'm have a bug where two arrays: 1. an array with data that can be manipulated 2. a similar one containing default data, which is used whenever I want to reset the first one They apparently "stick" together with synchronized values. When I manipulate array 1, array 2 changes too. This would be the old COPY problem, but I use COPY everywhere, whenever I need to create array 2. However I do frequently PICK values from array 2 and POKE it in array 1 at the same location to reset a specific location to a default value. Would that create a similar problem? |
Gabriele 30-Jul-2005 [1592x2] | it depends on the kind of value you are picking/poking. |
i guess you need copy/deep and poke ... copy pick ... | |
Henrik 30-Jul-2005 [1594] | I pick and poke arrays |
Gabriele 30-Jul-2005 [1595] | i mean, what is the result of PICK? i.e. what is inside the arrays? |
Volker 30-Jul-2005 [1596] | if the arrays have multiple dimensions, you need copy/deep. And if there are objects inside, those objects are not copied, then you need explicit work (copying all objects in a loop using 'make) |
Henrik 30-Jul-2005 [1597x2] | Volker, the arrays are indeed both 2 dimensional and contain objects. I'll try a different init method. |
gabriele, the arrays contain other arrays, hence PICK returns arrays of objects. | |
Gabriele 30-Jul-2005 [1599] | so, copy/deep, and, as volker suggests, you're likely to need to clone the objects too. |
Henrik 31-Jul-2005 [1600] | thanks, both. problem solved. :-) |
Henrik 6-Aug-2005 [1601] | a one liner I didn't see on rebol.com: Sum of all numbers in a block!: do do replace/all mold [1 2 3 4 5] " " " + " |
Sunanda 7-Aug-2005 [1602] | Clever! But crucially dependent on the block being contained on one line: xx: { do do replace/all mold [1 2 3 4 5] " " " + " } do xx But then you did call it a one-liner :-) |
Henrik 7-Aug-2005 [1603] | do trim/lines xx could fix that |
Volker 7-Aug-2005 [1604] | Is there a function which cuts the suffix, so i can go from %file.txt to %file.html ? I remember darkly there is one now. |
Henrik 7-Aug-2005 [1605] | I think there is one which splits a file up in an object with path, filename and extension. can't remember which though.... |
Volker 7-Aug-2005 [1606] | thats decode-url, good thought, but not in my case. |
Pekr 7-Aug-2005 [1607] | what about cutting at last occurance of "dot"? |
Volker 7-Aug-2005 [1608] | longer :) but thats what i do now. |
Pekr 7-Aug-2005 [1609x3] | >> prefix: func [filename][copy/part file find/last file "."] >> prefix file == %movie.cd1.divx-rel6 |
where - >> file: %movie.cd1.divx-rel6.avi | |
maybe 'prefix? would be good addition to Core, as we have 'suffix? already? :-) | |
Volker 7-Aug-2005 [1612] | thats cool, better than mine :) thanks. |
older newer | first last |