Page 1 of 1

replacing a period in a string using replace

Posted: Fri Feb 18, 2011 6:14 pm
by two-
i'm using textexpander on mac os x. the result of the following code is no output. nothing.

Code: Select all

#!/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:

Code: Select all

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

Code: Select all

#!/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:

Code: Select all

> (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:

Code: Select all

> (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:

Code: Select all

> (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.

Re: replacing a period in a string using replace

Posted: Fri Feb 18, 2011 7:34 pm
by Lutz
In this example:

Code: Select all

> (set 'text (exec "pbpaste"))
("Object.Oriented.Analysis.")
> (replace "\." text " " 0)
(" ")
you should do:

Code: Select all

> (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:

Code: Select all

> (replace "." "hello.world" " ")
"hello world"
> 
When using the regex option with curly braces, a single backslash is enough.

Code: Select all

> (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:

Code: Select all

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

Re: replacing a period in a string using replace

Posted: Fri Feb 18, 2011 10:51 pm
by two-
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?

Re: replacing a period in a string using replace

Posted: Fri Feb 18, 2011 11:11 pm
by itistoday
two- wrote:Can you have println print without the trailing newline?
Newlisp's manual is full of useful information.

Re: replacing a period in a string using replace

Posted: Sat Feb 19, 2011 12:47 am
by two-
Thank you, itistoday.