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

[REBOL] Re: My function seems to have no effect

From: joel:neely:fedex at: 15-Apr-2002 8:48

Hi, Richard, The definition of your GET_FUNC_NAME seens to want a string argument, but you're handing it a block. However, let's look a little deeper... COFFRE Richard FTO wrote:
> Hi Rebol fellows, > > Hereafter my code : > REBOL[] > get_func_name: make function! [ chaine ] [ > use [ guillemet ] [ > guillemet: make string! {"} > > remove/part chaine 6 > > foo: copy/part chaine 1 1 > if foo == guillemet [ remove/part chaine 1 ] > > ; Supprimer les 2 derniers caractères > clear skip tail chaine -2 > > return (chaine) > ] > ] >
1) You should establish types for function arguments whenever possible. That will help you catch any mismatch errors (whether typos or thinkos ;-). get_func_name: func [chaine [string!]] [ ;... ] 2) If you need a local value within a function, use the right mechanism for that purpose. There are some potentially confusing interactions between USE and FUNC which you can avoid by saying get_func_name: func [chaine [string!] /local guillemet] [ ;... ] to put GUILLEMET in the context of the GET_FUNC_NAME function. 3) That said, in this case you don't need GUILLEMET at all, since you're only using it as a constant value in the test to see whether to snip off the leading quote. You also don't need FOO (which you use but allow to remain global!) get_func_name: func [chaine [string!]] [ remove/part chaine 6 if chaine/1 = #"^"" [ remove/part chaine 1 ] ; Supprimer les 2 derniers caractères clear skip tail chaine -2 chaine ] because a string is just a series of characters, so you can explicitly test (e.g.) the first character of a string vs. a character value. 4) REMOVE defaults to removing one element, so the /PART refinement isn't needed for a single element removal. get_func_name: func [chaine [string!]] [ remove/part chaine 6 if chaine/1 = #"^"" [ remove chaine ] ; Supprimer les 2 derniers caractères clear skip tail chaine -2 chaine ] 5) CLEAR returns the tail of its argument series, and you apparently want the head of that series as your result, so why not... get_func_name: func [chaine [string!]] [ remove/part chaine 6 if chaine/1 = #"^"" [ remove chaine ] head clear skip tail chaine -2 ] 6) REMOVE returns the remainder of the series, so... get_func_name: func [chaine [string!]] [ if #"^"" = first remove/part chaine 6 [ remove chaine ] head clear skip tail chaine -2 ] After the above changes, we have this behavior
>> get_func_name "123456fonction rearzer r r12"
== "fonction rearzer r r" Is that what you wanted as the result for this test case? Finally, I don't know your application, but I'm a bit puzzled by the issue of checking for (and removing if found) the quotation mark. Do you really have data in your application that looks like 123456"fonction rearzer r r12 with an embedded quotation mark? If there's a quotation mark after the first six characters, is there a matching quotation mark later on in the line? If so, should it be removed as well (either before or after deleting the last two characters)? HTH! -jn- -- ; Joel Neely joeldotneelyatfedexdotcom REBOL [] do [ do func [s] [ foreach [a b] s [prin b] ] sort/skip do function [s] [t] [ t: "" foreach [a b] s [repend t [b a]] t ] { | e s m!zauafBpcvekexEohthjJakwLrngohOqrlryRnsctdtiub} 2 ]