replacing a period in a string using replace

Q&A's, tips, howto's
Locked
two-
Posts: 4
Joined: Fri Feb 18, 2011 10:37 am

replacing a period in a string using replace

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

Lutz
Posts: 5289
Joined: Thu Sep 26, 2002 4:45 pm
Location: Pasadena, California
Contact:

Re: replacing a period in a string using replace

Post 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)
"           "
>

two-
Posts: 4
Joined: Fri Feb 18, 2011 10:37 am

Re: replacing a period in a string using replace

Post 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?

itistoday
Posts: 429
Joined: Sun Dec 02, 2007 5:10 pm
Contact:

Re: replacing a period in a string using replace

Post by itistoday »

two- wrote:Can you have println print without the trailing newline?
Newlisp's manual is full of useful information.
Get your Objective newLISP groove on.

two-
Posts: 4
Joined: Fri Feb 18, 2011 10:37 am

Re: replacing a period in a string using replace

Post by two- »

Thank you, itistoday.

Locked