Mailing List Archive: 49091 messages
  • Home
  • Script library
  • AltME Archive
  • Mailing list
  • Articles Index
  • Site search
 

disappearing data?

 [1/6] from: rchristiansen:pop:isdfa:sei-it at: 10-Nov-2000 18:09


I've created an object with a function which converts data in another object into XML. But the function will only work ONCE. Why? Here is the console session...
>> translate-message: make object! [
[ [ xml-tags: [ [ ["author" "/author"] [ ["subject" "/subject"] [ ["date" "/date"] [ ["content" "/content"] [ ["messageID" "/messageID"] [ ] [ [ markup-data: func [ [ data [object!] [ ][ [ data-object: make data [] [ object-data: next first data-object [ output: "" [ for x 1 (length? xml-tags) 1 [ [ item: reform [rejoin ["data-object" "/" (first o bject-data)]] [ made-tag: rejoin ["" (build-tag [(xml-tags/1/1)] ) (do item) (build-tag [(xml-tags/1/2)])] [ xml-tags: next xml-tags [ object-data: next object-data [ append output made-tag [ output [ ] [ ] [ ]
>> >> >> >> >> message: make object! [
[ author: "Ryan C. Christiansen" [ subject: "This is the Subject of the Message" [ date: 27-Oct-2000/17:27:43-5:00 [ content: {This is the main body of the message} [ messageID: 20001027172734 [ ]
>> >> >> >> >> translate-message/markup-data message
== {<author>Ryan C. Christiansen</author><subject>This is the Subject of the Message</subject><date>27-Oct-2000/17:27:43-5:00</date...
>> translate-message/markup-data message
== none

 [2/6] from: rchristiansen:pop:isdfa:sei-it at: 10-Nov-2000 18:34


Why is the translate-message/markup-data function "reusable" in the following code.... translate-message: make object! [ markup-data: func [ data [object!] ][ xml-tags: [ ["author" "/author"] ["subject" "/subject"] ["date" "/date"] ["content" "/content"] ["messageID" "/messageID"] ] data-object: make data [] object-data: next first data-object output: "" for x 1 (length? xml-tags) 1 [ item: reform [rejoin ["data-object" "/" (first object-data)]] made-tag: rejoin ["" (build-tag [(xml-tags/1/1)]) (do item) (build-tag [(xml-tags/1/2)])] xml-tags: next xml-tags object-data: next object-data append output made-tag output ] ] ] BUT not in the following code... translate-message: make object! [ xml-tags: [ ["author" "/author"] ["subject" "/subject"] ["date" "/date"] ["content" "/content"] ["messageID" "/messageID"] ] markup-data: func [ data [object!] ][ data-object: make data [] object-data: next first data-object output: "" for x 1 (length? xml-tags) 1 [ item: reform [rejoin ["data-object" "/" (first object-data)]] made-tag: rejoin ["" (build-tag [(xml-tags/1/1)]) (do item) (build-tag [(xml-tags/1/2)])] xml-tags: next xml-tags object-data: next object-data append output made-tag output ] ] ] -Ryan

 [3/6] from: jeff:rebol at: 10-Nov-2000 17:36


Howdy, Ryan:
> Why is the translate-message/markup-data function "reusable" in the > following code....
<<quoted lines omitted: 9>>
> ["messageID" "/messageID"] > ]
Because the by having your static xml-tags block in the function it has the effect of doing: xml-tags: head xml-tags for each function call. Otherwise the function leaves the object's xml-tags pegged at the tail: . . .
> for x 1 (length? xml-tags) 1 [
. . .
> xml-tags: next xml-tags
Here's the basic effect: o1: make object! [ f: does [ b: [1 2 3] print mold b forall b [] ] ] o2: make object! [ b: [1 2 3] f: does [ print mold b forall b [] ] ]
>> o1/f
[1 2 3]
>> o1/f
[1 2 3]
>> o2/f
[1 2 3]
>> o2/f
[] ;- jeff

 [4/6] from: joel:neely:fedex at: 13-Nov-2000 4:39


Hi, Ryan, [rebol-bounce--rebol--com] wrote:
> I've created an object with a function which converts data in another > object into XML. But the function will only work ONCE. Why?
Because your Markup-Data function steps Xml-Tags through its series value (initialized to a 5-element block) and then leaves it positioned at the end. Therefore, on the next call, the Length? is zero. Either use a local (to the function) variable, initialized as in working-xml-tags: copy xml-tags and iterate over the copy, or add xml-tags: head xml-tags at the end of Markup-Data. -jn-
> Here is > the console session...
<<quoted lines omitted: 11>>
> [ data [object!] > [ ][
...
> [ for x 1 (length? xml-tags) 1 [
...
> [ xml-tags: next xml-tags > [ object-data: next object-data
<<quoted lines omitted: 4>>
> [ ] > >>
Your function Markup-Data essentially steps Xml-Tags through its series value, leaving it positioned at the end of the series. Since you never reset

 [5/6] from: joel::neely::fedex::com at: 13-Nov-2000 4:42


[rebol-bounce--rebol--com] wrote:
> Why is the translate-message/markup-data function "reusable" in the > following code.... >
Because the first thing that Markup-Data does is reset Xml-Tags to the beginning of the literal block that follows.
> translate-message: make object! [ > markup-data: func [
<<quoted lines omitted: 22>>
> ] > BUT not in the following code...
See previous message. The object! version is not causing Xml-Tags to be reset to the beginning of the series. It is initialized at object creation, modified the first time through the function, and then never reset to the beginning. -jn-

 [6/6] from: norsepower:uswest at: 10-Nov-2000 21:41


OK, I gotcha. So the "position" in the xml-tags block remains the same after the function moves the position to the tail UNLESS the xml-tags block is named again within the function. Therefore, it is better to name the xml-tags block within the function. Unless I do this... In order to be able to write another function within the object which can use the same xml-tags block instead of naming its own, I need to make each function return xml-tags to the head of the block at the end of the for loop. THEN I can have xml-tags outside the functions and useable by any functions within the object. (the above understanding was written by a non-programmer. can you tell?) -Ryan

Notes
  • Quoted lines have been omitted from some messages.
    View the message alone to see the lines that have been omitted