replacing a period in a string using replace

Started by two-, February 18, 2011, 10:14:57 AM

Previous topic - Next topic

two-

i'm using textexpander on mac os x. the result of the following code is no output. nothing.
#!/usr/bin/newlisp
(set 'text (exec "pbpaste"))
(replace {.} text " " 0)


I want to replace every occurance of the period {.} with a space " "...

The following code also produces no output:
#!/usr/bin/env newlisp
(set 'text (string (exec "pbpaste")))
(println (parse (replace {.} text " " 0) ""))


same:
#!/usr/bin/env newlisp
(set 'text (string (exec "pbpaste")))
(set 'text2 (parse (replace {.} text " ") ""))
(println text2)
(exit)

even if i use #!/usr/bin/newlisp in the prologue of the script, i still get nothing



1. yes i can execute newlisp in the shell with either /usr/bin/newlisp or /usr/bin/env newlisp .

2. yes, the textexpander macro definition type is set to shell script.



the result of evaluating the first code block above in newlisp from the shell when i copy the string "Object.Oriented.Analysis." (without double quotes) is:
> (set 'text (exec "pbpaste"))
("Object.Oriented.Analysis.")
> (replace "." text " " 0)
(" ")


the result of evaluating the second code block above in newlisp from the shell when i copy the string "Object.Oriented.Analysis." (without double quotes) is:
> (set 'text (string (exec "pbpaste")))
"("Object.Oriented.Analysis.")"
> (println (parse (replace {.} text " " 0) ""))
("                                    ")
("                                    ")


the result of evaluating the third code block above in newlisp from the shell when i copy the string "Object.Oriented.Analysis." (without double quotes) is:
> (set 'text (string (exec "pbpaste")))
"("Object.Oriented.Analysis.")"
> (set 'text2 (parse (replace {.} text " ") ""))
("("Object Oriented Analysis ")")
> (println text2)
("("Object Oriented Analysis ")")
("("Object Oriented Analysis ")")
^ this seems better, but textexpander produces no output.

Lutz

#1
In this example:


> (set 'text (exec "pbpaste"))
("Object.Oriented.Analysis.")
> (replace "." text " " 0)
(" ")

you should do:


> (set 'text (exec "pbpaste"))
("Object.Oriented.Analysis.")    ; <-- return value from exec contains a list
> (replace "\." (first text) " " 0)

The return from 'exec' is never a string, but a list of strings. Also note the double backslash when using quotes to limit the key string.



You could use 'replace' without the regex option number to avoid backslashing the dot. As a simple text replacement this will also be faster:



> (replace "." "hello.world" " ")
"hello world"
>

When using the regex option with curly braces, a single backslash is enough.


> (replace {.} "hello.world" " " 0)
"hello world"
>

When you omit the backslash before the point but use the regex option (as in your second example), then you say: "replace any character" - and end up with string of spaces:


> (replace {.} "hello.world" " " 0)
"           "
>

two-

#2
Oh. Thank you ;}.

So, the return value of (exec "pbpaste") is a list (of strings), and in this case the first element of that list is the string "Object.Oriented.Analysis."



Can you have println print without the trailing newline?

itistoday

#3
Quote from: "two-"Can you have println print without the trailing newline?


http://www.newlisp.org/downloads/newlisp_manual.html#print">Newlisp's manual is full of useful information.
Get your Objective newLISP groove on.

two-

#4
Thank you, itistoday.