Skip to content
Related Articles

Related Articles

Perl | y Operator

View Discussion
Improve Article
Save Article
  • Last Updated : 07 May, 2019
View Discussion
Improve Article
Save Article

The y operator in Perl translates all characters of SearchList into the corresponding characters of ReplacementList.
Here the SearchList is the given input characters which are to be converted into the corresponding characters given in the ReplacementList.

Syntax: y/SearchList/ReplacementList/

Returns: the translated string

Example 1: This example uses y operator for translating from lower case to upper case.




#!/usr/bin/perl
   
# Initialising some strings
$string1 = 'gfg is a computer science portal';
$string2 = 'geeksforgeeks';
  
# Calling to y function
$string1 =~ y/a-z/A-Z/;
$string2 =~ y/a-z/A-Z/;
  
# Getting translated strings
print "$string1\n";
print "$string2\n";


Output:

GFG IS A COMPUTER SCIENCE PORTAL
GEEKSFORGEEKS

Example 2: This example uses y operator for translating from upper case to lower case.




#!/usr/bin/perl
   
# Initialising some strings
$string1 = 'GFG IS A COMPUTER SCIENCE PORTAL';
$string2 = 'GEEKSFORGEEKS';
  
# Calling to y function
$string1 =~ y/A-Z/a-z/;
$string2 =~ y/A-Z/a-z/;
  
# Getting translated strings
print "$string1\n";
print "$string2\n";

Output :

gfg is a computer science portal
geeksforgeeks

Note: This y operator does the task of lc() function and uc() function as well as it translates the input characters into numeric form etc.

Example 3: This example uses y operator for translating from upper case to numeric form.




#!/usr/bin/perl
   
# Initialising some strings
$string1 = 'GFG IS A COMPUTER SCIENCE PORTAL';
$string2 = 'GEEKSFORGEEKS';
  
# Calling to y function
$string1 =~ y/A-Z/0-9/;
$string2 =~ y/A-Z/0-9/;
  
# Getting translated strings
print "$string1\n";
print "$string2\n";

Output :

656 89 0 29999949 9284924 999909
6449959964499

My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!