Core Function Translation

From Sputnik Wiki
Jump to: navigation, search
<Expression> =~ tr/searchList/replacementList/flags
<Expression> =~ y/searchList/replacementList/flags
tr/searchList/replacementList/flags
y/searchList/replacementList/flags

Contents

Description

Translate characters from one character set to another similar to StrTr() function.

Parameters

<Expression>

Any valid expression that is a string or an array.

You may choose to skip this part and it will use $_ for you.

searchList

A pattern to use to create the searchList see Remarks below.

This list will become a sequence of characters to be replaced.

replacementList

A pattern to use to create the replacementList see Remarks below.

This list will become a sequence of characters to be replace the corresponding character (at the same index number) in the searchList.

flags

Optional; The flags to use in the operation.

i = Ignore case.

d - Delete found but not replaced characters

o = Do not recompile the character sets after the first compile (Improves speed of your function if you run the operation many times).

s = Makes it so the sequences of characters that were transliterated to the same character are squashed down to a single instance of the character

r = Non-destructive it will not modify the original item at all but instead return a modified copy of it as part of a return array

p = Allows the searchList to be parsed as if it was a "" string so variables etc will be properly parsed and inserted

P = Allows the replacementList to be parsed as if it was a "" string so variables etc will be properly parsed and inserted

Note - p and P work differently in Regexp there they will disable parsing as a string but here p and P enable parsing as a string just something to keep in mind.

Default: None of the flags are enabled by default

Return Value

Returns number of characters replaced or deleted.

OR if /r flag is used it will return an array where first element of the number of characters replaced or deleted and the second element is a clone of the input with the translation applied.

Remarks

The Sputnik tr/// function is used to transliterate all occurrences found in a search list with the corresponding character in a replacement list.

This function is a character by character translation and if you want to perform more complex operations you can use the <Expression> =~ s/pattern/replacement/flags regex substitution operator instead.

In Sputnik tr/// is known better as an operator rather than a function.

The syntax form of the tr/// operator is as follows:

tr/SEARCHLIST/REPLACEMENTLIST/od
# this exactly the same
y/SEARCHLIST/REPLACEMENTLIST/od
# you choose if you want to use tr/ or y/

It replaces all the occurrences of the characters in SEARCHLIST with the characters in the REPLACEMENTLIST.

It returns the number of characters replaced or deleted.

The transliteration table is built at run time, so you can use interpolation either in SEARCHLIST or in REPLACEMENTLIST including variables as long as you set the appropriate flag for that.

The o, d are modifiers that add additional functionality to the Sputnik tr/// operator (see Flags above).

The strings can be specified via the =~. Please note that Sputnik's tr/// operator does not do regular expressions character classes such as \d.

One character that has a special meaning to tr/// is the dash (-) and it indicates a range of characters (for example tr/A-C/a-c/).

The slash (/) has the meaning of a delimiter, because it separates the arguments of the tr/// operator.

tr// like all Sputnik functions fully supports Unicode.

Example

Simple convert lower case into uppercase

my $string = 'one two three four five six seven';
$changed = $string =~ tr/a-z/A-Z/;
 
say $string;
say $changed;
# it prints: ONE TWO THREE FOUR FIVE SIX SEVEN
#            27

Example of using the /r flag to return a modified copy instead of modifying the original

my $string = 'one two three four five six seven';
say "Original string: $string";
List ($changed, $newString) = $string =~ tr/a-z/A-Z/r;
 
say "Original string (after tr): $string";
say "New string: $newString";
say "Change count: $changed";
# It Prints:
# Original string: one two three four five six seven
# Original string (after tr): one two three four five six seven
# New string: ONE TWO THREE FOUR FIVE SIX SEVEN
# Change count: 27

Simple convert lower case into uppercase but this time using a variable (possible by the /p flag)

my $string = 'one two three four five six seven';
my $s = 'a'..'z';
my $changed = $string =~ tr/$s/A-Z/p;
 
say $string;
say $changed;
# it prints: ONE TWO THREE FOUR FIVE SIX SEVEN

Removing something you didn't want and replacing with spaces

my $string = "123.95,44.32,27.77,221q23";
$string =~ tr/,.q/   /;
 
printr $string;
# it prints: 123 95 44 32 27 77 221 23

Converting and removing stuff

my $string = "aa bbb c dd eee ff mmmm rr";
$string =~ tr/a-z/1234/d;
 
printr $string;
# it prints: 11 222 3 44

Example of using /s to squish

my $string = "123,,,95,,44,,32,,,,27....77";
$string =~ tr/,.//s;
print $string;
# it prints: 123,95,44,32,27.77

Another example of using /s to squish

my $string = "This . is an 7example 9only";
$string =~ tr/ a-zA-Z.79/\-a-zA-Z/ds;
 
printr $string;
# it prints: This-is-an-example-only
# Notice it will remember what it did previously and still squish?

Another example of counting the number of times a character appears in a string

my $string = "This pencil is .... old. You can ..... it .....";
$dots = $string =~ tr/././;
 
say "Dots '$dots' String '$string'";
# it prints: Dots '15' String 'This pencil is .... old. You can ..... it .....'
# notice the string remains unmodified?

Ignore case

my $string = 'one TWO three four five six seven';
$changed = $string =~ tr/a-z/0-9/i;
 
say $string;
say $changed;
# it prints: 994 999 97944 5999 5894 989 94949
#            27

Using it on an array

my $string = array("The", "quick", "brown", "fox");
$string =~ tr/a-z/A-Z/;
 
printr $string;
# it prints:
#ARRAY
#{
#        [0] => THE
#        [1] => QUICK
#        [2] => BROWN
#        [3] => FOX
#}

If you ignore the $var =~ part and just do the translation part it will use the $_ variable example

my $_ = "cat";
tr/a-z/A-Z/;
say $_; // Prints CAT
Personal tools
Namespaces
Variants
Actions
Navigation
Toolbox