Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

Re: How to replace \t with \s

by Don Coyote (Hermit)
on Jan 20, 2021 at 15:56 UTC ( [id://11127152]=note: print w/replies, xml ) Need Help??


in reply to How to replace \t with \s

tr replaces a list of characters with another list of characters, so without modifiers this will not do as you think, it will only replace \t with the first incident of the replacement list.

my $word = q{thing}; $word =~ tr/i/oooo/; __END__ thong

Use the substitution operator s///. This will replace a match with a replacement string.

my $word = q{Perl is a thing I like to do.}; $word =~ s/i/oooo/; __END__ Perl oooos a thing I like to do.

Or globally on the line.

$word =~ s/i/oooo/g; __END__ Perl oooos a thoooong I looooke to do.

Splitting on a repeated character singularly will produce multiple empty lists, that once replaced will essentially reconstruct the line as it was processed. Which is fine if that is what you want.

my @text = split /o/, $word; print join qq{o},@text; __END__ Perl oooos a thoooong I looooke to do.

This can be seen more easily joining with a linefeed character\n.

my @text = split /o/, $word; print join qq{/n},@text; __END__ Perl s a th ng I l ke t d .

Also note, join uses a literal string as a first argument. So you are not able to use a match escape for general whitespace to replace a literal character escape for a particular whitespace character.

\t is a tab whitespace escape, that interpolates in double-quoted context.

\s is a matching character escape that matches against any whitespace literal.

\s would not interpolate inside double-quoted context as how would perl know to which whitespace literal you wanted to replace with?

Whereas split also has the option to use the match m// operator in place of a literal string as its first argument. Moreover, literal ' ' space string to split alters splits behaviour in regard to white space parsing.

Your goal is not clear about if that is your attempt to solve the problem or if that is something you are preparing the document for so that you can then perform. As such, depending on what it is you want to do you may be able to make use of a capturing split, or splitting directly on the tabs instead of spaces on the first pass.

my $line = q{Perl is a thing I like to do.}; print split /i/, $line; print split /(i)/, $line; __END__ Perl s a thng I lke to do. Perl is a thing I like to do.
print join q{o}, split /i/, $line; print join q{o}, split /(i)/, $line; __END__ Perl os a thong I loke to do. Perl oios a thoiong I loioke to do.

And splitting on only incidents of only two or more consecutive characters.

my $line = q{Perl is a thiiing I like to do.}; print join q{ooo}, split /i{2,}/, $line; __END__ Perl is a thooong I like to do.

This also has the advantage of not actually mangling your documents in place. There could be loose tab characters anywhere in all those files, so set up some small test lines to ensure the operator behaves a expected, and make backups. As per usual.

edit: Clarified splits usage of string or match operator as first argument, and that \s does not interpolate.


Dooon Coooyoooteee

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://11127152]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others having a coffee break in the Monastery: (2)
As of 2024-04-19 19:48 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found