tr// versus s///g

--0-936931774-1268062028=:957
Content-Type: text/plain; charset=iso-8859-1
Content-Transfer-Encoding: quoted-printable

Does tr/'\n'/' '/ do the same as tr/'\n'/' '/g?=A0 ( replace newline with s=
pace )
If so, is one preferred over the other?
=A0=0A=0A=0A
--0-936931774-1268062028=:957--
Tony Esposito [ Mo, 08 März 2010 16:27 ] [ ID #2034354 ]

Re: tr// versus s///g

Tony Esposito wrote:
> Does tr/'\n'/' '/ do the same as tr/'\n'/' '/g? ( replace newline with space )
> If so, is one preferred over the other?

Neither of the above is correct. The correct forms are:

tr/\n/ /;
s/\n/ /g;


--
Just my 0.00000002 million dollars worth,
Shawn

Programming is as much about organization and communication
as it is about coding.

I like Perl; it's the only language where you can bless your
thingy.

Eliminate software piracy: use only FLOSS.

--
To unsubscribe, e-mail: beginners-unsubscribe [at] perl.org
For additional commands, e-mail: beginners-help [at] perl.org
http://learn.perl.org/
Shawn H Corey [ Mo, 08 März 2010 16:38 ] [ ID #2034357 ]

Re: tr// versus s///g

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.




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-unsubscribe [at] perl.org
For additional commands, e-mail: beginners-help [at] perl.org
http://learn.perl.org/
jwkrahn [ Mo, 08 März 2010 16:50 ] [ ID #2034358 ]

Re: tr// versus s///g

--0-1186463490-1268069538=:32337
Content-Type: text/plain; charset=iso-8859-1
Content-Transfer-Encoding: quoted-printable

You miss my point but thanks for the syntax check.=A0 I am concerned with c=
omparing the functionality, one versus the other.
=A0
Thanks.

--- On Mon, 8/3/10, Shawn H Corey <shawnhcorey [at] gmail.com> wrote:


From: Shawn H Corey <shawnhcorey [at] gmail.com>
Subject: Re: tr// versus s///g
To: "Tony Esposito" <tony1234567893 [at] yahoo.co.uk>
Cc: "Beginners Perl" <beginners [at] perl.org>
Date: Monday, 8 March, 2010, 9:38


Tony Esposito wrote:
> Does tr/'\n'/' '/ do the same as tr/'\n'/' '/g?=A0 ( replace newline with=
space )
> If so, is one preferred over the other?

Neither of the above is correct.=A0 The correct forms are:

tr/\n/ /;
s/\n/ /g;


--
Just my 0.00000002 million dollars worth,
=A0 Shawn

Programming is as much about organization and communication
as it is about coding.

I like Perl; it's the only language where you can bless your
thingy.

Eliminate software piracy:=A0 use only FLOSS.

--
To unsubscribe, e-mail: beginners-unsubscribe [at] perl.org
For additional commands, e-mail: beginners-help [at] perl.org
http://learn.perl.org/


=0A=0A=0A
--0-1186463490-1268069538=:32337--
Tony Esposito [ Mo, 08 März 2010 18:32 ] [ ID #2034360 ]

Re: tr// versus s///g

--0-40461381-1268069722=:80505
Content-Type: text/plain; charset=iso-8859-1
Content-Transfer-Encoding: quoted-printable

Sorry about the syntax errors ... Yes, just want to replace all newline cha=
racters with a space and since tr does not have g (global) 'option' I was t=
hinking that s/\n/ /g is actually better. Or does tr do a global translate =
by default?
=A0
Thanks
--- On Mon, 8/3/10, John W. Krahn <jwkrahn [at] shaw.ca> wrote:


From: John W. Krahn <jwkrahn [at] shaw.ca>
Subject: Re: tr// versus s///g
To: "Perl Beginners" <beginners [at] perl.org>
Date: Monday, 8 March, 2010, 9:50


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

Replace every ' with ' and every "\n" with " " and every ' with '. Should p=
robably 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///.=
=A0 That will replace the string "'\n'" with the string "' '" globally but =
I assume you meant s/\n/ /g.


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

tr/// is usually more efficient (faster) than s///g.=A0 Unless you really m=
eant to use the string "'\n'" because tr/// cannot do strings, just charact=
ers.




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

-- To unsubscribe, e-mail: beginners-unsubscribe [at] perl.org
For additional commands, e-mail: beginners-help [at] perl.org
http://learn.perl.org/


=0A=0A=0A
--0-40461381-1268069722=:80505--
Tony Esposito [ Mo, 08 März 2010 18:35 ] [ ID #2034361 ]

Re: tr// versus s///g

Tony Esposito wrote:
>
>> On Mon, 8/3/10, John W. Krahn <jwkrahn [at] 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-unsubscribe [at] perl.org
For additional commands, e-mail: beginners-help [at] perl.org
http://learn.perl.org/
jwkrahn [ Mo, 08 März 2010 18:52 ] [ ID #2034362 ]

Re: tr// versus s///g

>>>>> "TE" =3D=3D Tony Esposito <tony1234567893 [at] yahoo.co.uk> writes:

TE> You miss my point but thanks for the syntax check.=A0 I am concerned
TE> with comparing the functionality, one versus the other.

they are not comparable at all. tr/// works on individual characters and
nothing else. s/// works with regexes which can modify strings as a
whole. there are a few simple cases where one can do the same as the
other such as replacing a single char with another globally. and in that
case tr/// wins for speed and better specifity.

the reason people conflate the two is becaus they share the =3D~ binding
op. i have seen op tables in web tutorials that incorrectly list tr///
as a regex op.

so think tr/// for chars and only chars and s/// for regexes and
strings. simple.

uri

--
Uri Guttman ------ uri [at] stemsystems.com -------- http://www.sysarch.com =
--
----- Perl Code Review , Architecture, Development, Training, Support ----=
--
--------- Gourmet Hot Cocoa Mix ---- http://bestfriendscocoa.com -------=
--

--
To unsubscribe, e-mail: beginners-unsubscribe [at] perl.org
For additional commands, e-mail: beginners-help [at] perl.org
http://learn.perl.org/
Uri Guttman [ Mo, 08 März 2010 18:54 ] [ ID #2034363 ]
Perl » gmane.comp.lang.perl.beginners » tr// versus s///g

Vorheriges Thema: Controlling one process depending on the status of another
Nächstes Thema: Test if directory is not empty