Tony Esposito wrote:

On Mon, 8/3/10, John W. Krahn <jwkr...@shaw.ca> wrote:

Tony Esposito wrote:
Does tr/'\n'/' '/

Replace every ' with ' and every "\n" with " " and every ' with '.
Should probably just be tr/\n/ /.

do the same as tr/'\n'/' '/g?

I assume you mean s/'\n'/' '/g because /g is not a valid option for
tr///.  That will replace the string "'\n'" with the string "' '"
globally but I assume you meant s/\n/ /g.

  ( replace newline with space )
If so, is one preferred over the other?

tr/// is usually more efficient (faster) than s///g.  Unless you
really meant to use the string "'\n'" because tr/// cannot do strings,
just characters.

Sorry about the syntax errors ... Yes, just want to replace all
newline characters with a space and since tr does not have g (global)
'option' I was thinking that s/\n/ /g is actually better. Or does tr
do a global translate by default?

Yes, tr/// operates on the entire string. There is no way to limit it to only part of the string. tr/// transliterates characters on the left to characters on the right.

s/// uses pattern matching (regular expressions) and does interpolation just like any double quoted string so it has to do a lot more work than tr///.

So, if you are only interested in manipulating characters then tr/// may be all that you need.



John
--
The programmer is fighting against the two most
destructive forces in the universe: entropy and
human stupidity.               -- Damian Conway

--
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/


Reply via email to