Skip to content
geeksforgeeks
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • DSA
  • Practice Problems
  • Python
  • C
  • C++
  • Java
  • Courses
  • Machine Learning
  • DevOps
  • Web Development
  • System Design
  • Aptitude
  • Projects
Open In App
Next Article:
Perl | oct() Function
Next article icon

Perl | List Functions

Last Updated : 07 May, 2019
Comments
Improve
Suggest changes
Like Article
Like
Report

A list in Perl is a collection of scalar values. We can access the elements of a list using indexes. Index starts with 0 (0th index refers to the first element of the list). We use parenthesis and comma operators to construct a list. In Perl, scalar variables start with a $ symbol whereas list variables start with @ symbol.
List provides various pre-defined Functions to perform operations with ease. Some of these Functions are as below:

  • join() function
    join() function is used to combine the elements of a List into a single string with the use of a separator provided to separate each element. This function returns the joined string. A separator can work only between the pairs. A separator can’t be placed at either end of the string. In order to join the strings without a separator, an empty parameter is passed to the function.

    Syntax: join(Separator, List)

    Parameter:

    • Separator: provided to separate each element while joining
    • List: to be converted to single String

    Returns: a joined String

    Example :




    #!/usr/bin/perl
      
    # Initializing list with alphabets A to Z
    @list = (A..Z);
      
    # Printing the original list
    print "List: @list\n";
      
    # Using join function introducing
    # hyphen between each alphabets
    print "\nString after join operation:\n";
    print join("-", @list);
     
     

    Output:

      List: A B C D E F G H I J K L M N O P Q R S T U V W X Y Z    String after join operation:  A-B-C-D-E-F-G-H-I-J-K-L-M-N-O-P-Q-R-S-T-U-V-W-X-Y-Z  
  • reverse() function
    Reverse() function in Perl returns the elements of List in reverse order in a list context. While in a scalar context, it returns a concatenated string of the values of List, with all characters in opposite order. It returns String in Scalar Context and List in List Context.

    Syntax: reverse List

    Parameter:
    List: list to be reversed

    Returns: elements in reverse order

    Example:




    # Initializing a list
    @list = ("Raj", "E123", 12000);
      
    # Reversing the list
    @rname = reverse(@list);
      
    # Printing the reversed list
    print "Reversed list is @rname";
      
    # Initializing a scalar
    $string = "GeeksforGeeks";
      
    # Reversing a scalar
    $r = reverse($string);
    print "\nReversed string is $r";
     
     

    Output:

      Reversed list is 12000 E123 Raj  Reversed string is skeeGrofskeeG  
  • map() function
    map() function in Perl evaluates the operator provided as a parameter for each element of List. For each iteration, $_ holds the value of the current element, which can also be assigned to allow the value of the element to be updated. map() function runs an expression on each element of an array and returns a new array with the updated results. It returns the total number of elements generated in scalar context and list of values in list context.

    Syntax: map(operation, List)
    Parameter:

    • operation: to be performed on list elements
    • List: whose elements need to be changed

    Example :




    # Initializing a list
    @Dept = ('comp', 'inft', 'extc', 'mech');
      
    # Converting first character capital
    @upd1 = map(ucfirst, @Dept);
      
    # Printing list
    print "List with First char capital: ";
    foreach $i (@upd1) 
    {
       print "$i, ";
    }
      
    # Converting all characters capital
    @upd2 = map(uc, @Dept);
      
    # Printing list
    print "\nList with all char capital: ";
    foreach $i (@upd2) 
    {
       print "$i, ";
    }
     
     

    Output:

    List with First char capital: Comp, Inft, Extc, Mech,   List with all char capital: COMP, INFT, EXTC, MECH, 
  • sort() function
    sort() function in Perl is used to arrange the List according to the condition of sorting specified to the function. If no condition is specified, then it sorts according to normal alphabetical sequence.
    If a condition is specified then the function sorts the List according to the condition.

    Syntax: sort(condition, List)

    Example




    # Initializing two lists
    @country = ('India', 'Qatar', 'Bangladesh', 'France', 'Italy');
    @capital = ('Delhi', 'Lahore', 'Dhaka', 'Paris', 'Rome');
      
    # Printing countries in sorted order
    print"Countries in sorted order: \n";
    print sort @country;
    print "\n";
      
    # Printing sorted country and capital values
    print "\nCombining both the lists in sorted order:\n";
    print sort @country, @capital;
    print "\n";
      
    # Initializing a list with number
    @list = (19, 4, 54, 33, 99, 2);
      
    # Sorting in descending order
    @s = sort{$b <=> $a} @list;
    print "\nPrinting numbers in sorted order:\n";
    foreach $i(@s)
    {
        print "$i, ";
    }
     
     

    Output:

    Countries in sorted order:   BangladeshFranceIndiaItalyQatar    Combining both the lists in sorted order:  BangladeshDelhiDhakaFranceIndiaItalyLahoreParisQatarRome    Printing numbers in sorted order:  99, 54, 33, 19, 4, 2, 


Next Article
Perl | oct() Function

R

rupanisweety
Improve
Article Tags :
  • Perl
  • Perl-Arrays
  • perl-list
  • Perl-List-Functions

Similar Reads

  • Perl | int() function
    int() function in Perl returns the integer part of given value. It returns $_ if no value provided. Note that $_ is default input which is 0 in this case. The int() function does not do rounding. For rounding up a value to an integer, sprintf is used. Syntax: int(VAR) Parameters: VAR: value which is
    1 min read
  • Perl | log() Function
    log() function in Perl returns the natural logarithm of value passed to it. Returns $_ if called without passing a value. log() function can be used to find the log of any base by using the formula: Syntax: log(value) Parameter: value: Number of which log is to be calculated Returns: Floating point
    2 min read
  • Perl | oct() Function
    oct() function in Perl converts the octal value passed to its respective decimal value. For example, oct('1015') will return '525'. This function returns the resultant decimal value in the form of a string which can be used as a number because Perl automatically converts a string to a number in nume
    1 min read
  • Perl | length() Function
    length() function in Perl finds length (number of characters) of a given string, or $_ if not specified. Syntax:length(VAR) Parameter: VAR: String or a group of strings whose length is to be calculated Return: Returns the size of the string. Example 1: #!/usr/bin/perl # String whose length is to be
    1 min read
  • Perl | keys() Function
    keys() function in Perl returns all the keys of the HASH as a list. Order of elements in the List need not to be same always, but, it matches to the order returned by values and each function. Syntax: keys(HASH) Parameter: HASH: Hash whose keys are to be printed Return: For scalar context, it return
    1 min read
  • Perl | glob() Function
    glob() function in Perl is used to print the files present in a directory passed to it as an argument. This function can print all or the specific files whose extension has been passed to it. Syntax: glob(Directory_name/File_type); Parameter: path of the directory of which files are to be printed. R
    1 min read
  • Perl | join() Function
    join() function in Perl combines the elements of LIST into a single string using the value of VAR to separate each element. It is effectively the opposite of split. Note that VAR is only placed between pairs of elements in the LIST; it will not be placed either before the first element or after the
    1 min read
  • Perl | ord() Function
    The ord() function is an inbuilt function in Perl that returns the ASCII value of the first character of a string. This function takes a character string as a parameter and returns the ASCII value of the first character of this string. Syntax: ord string Parameter: This function accepts a single par
    1 min read
  • Functions in LISP
    A function is a set of statements that takes some input, performs some tasks, and produces the result. Through functions, we can split up a huge task into many smaller functions. They also help in avoiding the repetition of code as we can call the same function for different inputs. Defining Functio
    3 min read
  • Perl | hex Function
    The hex function in Perl converts the given hexadecimal number ( of base 16 ) into its equivalent decimal number ( of base 10 ). Syntax: hex number Parameters: number: hexadecimal number to be converted Returns: equivalent decimal number of the given hexadecimal number. Example 1: #!/usr/bin/perl #
    1 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences