Mohd Mohtashim has Published 238 Articles

Creating Hashes in Perl

Mohd Mohtashim

Mohd Mohtashim

Updated on 29-Nov-2019 05:40:16

203 Views

Perl Hashes are created in one of the two following ways. In the first method, you assign a value to a named key on a one-by-one basis −$data{'John Paul'} = 45; $data{'Lisa'} = 30; $data{'Kumar'} = 40;In the second case, you use a list, which is converted by taking individual ... Read More

Selecting Elements from Lists in Perl

Mohd Mohtashim

Mohd Mohtashim

Updated on 29-Nov-2019 05:31:38

501 Views

The list notation in Perl is identical to that for arrays. You can extract an element from an array by appending square brackets to the list and giving one or more indices −Example Live Demo#!/usr/bin/perl $var = (5, 4, 3, 2, 1)[4]; print "value of var = $var"OutputThis will produce the ... Read More

Merging Arrays in Perl

Mohd Mohtashim

Mohd Mohtashim

Updated on 29-Nov-2019 05:27:58

2K+ Views

Because an array in Perl is just a comma-separated sequence of values, you can combine them together as shown below −Example Live Demo#!/usr/bin/perl @numbers = (1, 3, (4, 5, 6)); print "numbers = @numbers";OutputThis will produce the following result −numbers = 1 3 4 5 6The embedded arrays just become a ... Read More

The $[ Special Variable in Perl

Mohd Mohtashim

Mohd Mohtashim

Updated on 29-Nov-2019 05:13:56

443 Views

Perl provides numerous special variables, which have their predefined meaning.We have a special variable, which is written as $[. This special variable is a scalar containing the first index of all arrays. Because Perl arrays have zero-based indexing, $[ will almost always be 0. But if you set $[ to ... Read More

Sorting Arrays in Perl

Mohd Mohtashim

Mohd Mohtashim

Updated on 29-Nov-2019 05:11:45

811 Views

The sort() function in Perl sorts each element of an array according to the ASCII Numeric standards. This function has the following syntax −Syntaxsort [ SUBROUTINE ] LISTThis function sorts the LIST and returns the sorted array value. If SUBROUTINE is specified then specified logic inside the SUBROUTINE is applied ... Read More

Transform Perl Arrays to Strings

Mohd Mohtashim

Mohd Mohtashim

Updated on 29-Nov-2019 05:09:28

9K+ Views

We can use the join() function in Perl to rejoin the array elements and form one long scalar string. This function has the following syntax −Syntaxjoin EXPR, LISTThis function joins the separate strings of LIST into a single string with fields separated by the value of EXPR and returns the ... Read More

Transform Perl Strings into Arrays

Mohd Mohtashim

Mohd Mohtashim

Updated on 29-Nov-2019 05:07:22

2K+ Views

Let's look into a Perl function called split(), which has the following syntax −Syntaxsplit [ PATTERN [ , EXPR [ , LIMIT ] ] ]This function splits a string into an array of strings and returns it. If LIMIT is specified, splits into at most that number of fields. If ... Read More

Replacing Array Elements in Perl

Mohd Mohtashim

Mohd Mohtashim

Updated on 29-Nov-2019 05:04:20

671 Views

Now we are going to introduce one more function called splice(), which has the following syntax −Syntaxsplice @ARRAY, OFFSET [ , LENGTH [ , LIST ] ]This function will remove the elements of @ARRAY designated by OFFSET and LENGTH, and replaces them with LIST if specified. Finally, it returns the ... Read More

Slicing Array Elements in Perl

Mohd Mohtashim

Mohd Mohtashim

Updated on 28-Nov-2019 09:46:37

2K+ Views

You can also extract a "slice" from an array - that is, you can select more than one item from an array in order to produce another array.Example Live Demo#!/usr/bin/perl @days = qw/Mon Tue Wed Thu Fri Sat Sun/; @weekdays = @days[3, 4, 5]; print "@weekdays";OutputThis will produce the following result ... Read More

Adding and Removing Elements in Perl Array

Mohd Mohtashim

Mohd Mohtashim

Updated on 28-Nov-2019 09:44:27

3K+ Views

Perl provides a number of useful functions to add and remove elements in an array. You may have a question what is a function? So far you have used the print function to print various values. Similarly, there are various other functions or sometimes called subroutines, which can be used ... Read More

Advertisements