Perl | qw Operator

The qw operator in Perl is used to extract each element of the given string as it is in an array of elements in single-quote ( ‘ ‘ ).
This function stands for quote word because it considers each word of the given string as it is quoted like qw(Geeks for Geeks) is equivalent to (‘Geeks’, ‘for’, ‘Geeks’).
This qw() uses parentheses so it seems like it is a function but it is not. It uses different types of delimiters which are shown below:

@String = qw/Ram is a boy/;
@String = qw{Geeks for Geeks};
@String = qw[Geeks for Geeks];
@String = qw'Geeks for Geeks';
@String = qw"Geeks for Geeks";
@String = qw!Geeks for Geeks!;
@String = qw@Geeks for Geeks@;

Syntax: qw(String)

Parameters:
String : It is the input string whose each element is extracted.



Returns: each element of the given string as it is an array of elements in single-quote (”).

Example 1:

filter_none

edit
close

play_arrow

link
brightness_4
code

#!/usr/bin/perl
  
# Initialising a String as the parameter of qw
# operator whose each element is extracted.
@String = qw(GfG is a computer science portal);
foreach $key (@String) {
   print"Element is: $key\n";
}

chevron_right



Output:

Element is: GfG
Element is: is
Element is: a
Element is: computer
Element is: science
Element is: portal

Example 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

#!/usr/bin/perl
  
# Initialising a String as the parameter of qw
# operator whose each element is extracted.
@String = qw(Delhi Mumbai Kolkata Patna);
foreach $key (@String) {
   print"City name is: $key\n";
}

chevron_right


Output :

City name is: Delhi
City name is: Mumbai
City name is: Kolkata
City name is: Patna

Note: The qw operator reads a white-space and extract elements before and after the white-space.



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.