Found 157 Articles for PERL

How to count the number of matches in a Perl string?

Mukul Latiyan
Updated on 04-Apr-2023 15:50:30

529 Views

In Perl, we can find the number of matches in a string by different approaches. In this tutorial, we will discuss the three most widely used approaches. Searching for a Single Character in a Perl String Let's first take the case where we would want to search for a single character pattern in a string. For example, let's suppose we have a string that look something like this − "India.Japan.Russia.USA.China" And, we want to find the number of times "." (dot) appears in the above string. Example Consider the code shown below. my $countries ... Read More

How to count the number of keys in a Perl hash?

Mukul Latiyan
Updated on 04-Apr-2023 15:53:37

1K+ Views

There are times when we would want to know how many key-value pairs are present in a hash. These key-value pair counts are also known as the size of the hash. In Perl, we can find the number of keys in a Perl hash by using the "scalar" keyword or "keys" keyword. In this tutorial, we will explore two Perl examples where we will calculate the number of keys in a hash. Example Consider the code shown below. In this code, we have declared a hash named "countries" and in that hash, we have different countries, each having ... Read More

How to convert binary to decimal and vice versa in Perl?

Mukul Latiyan
Updated on 04-Apr-2023 15:48:37

494 Views

The conversion between binary to decimal and decimal to binary is a necessity when it comes to dealing with binary data and using them in some simple applications. In Perl, we can convert from decimal to binary and vice versa in multiple ways. In this tutorial, we will explore different examples where we will first convert a decimal value into a binary value and then a binary value into a decimal value. Decimal to Binary in Perl Let's first see an example where we are given two decimal values and we want to convert them to their binary representation. ... Read More

How to convert a string to a number in Perl?

Mukul Latiyan
Updated on 04-Apr-2023 15:47:18

2K+ Views

In Perl, we can convert a string into a number by appending the integer value 0 in front of it. There are some other approaches too, which we will discuss in this tutorial with the help of examples. Example Let's first consider the case where the string that we are trying to convert looks something like "123abc" or "23Hzbc". You can see that the it is a mixture of both integer values and characters. Consider the code shown below. my $firstStr = '23ASDF'; print "Convering First String to Number: ", $firstStr + 0; print ""; my $secondStr = ... Read More

How to compare two strings in Perl?

Mukul Latiyan
Updated on 04-Apr-2023 15:46:09

4K+ Views

Perl has methods and operators that determine whether two string values are equal or different. In Perl, the compare strings function is essential for comparing two strings and their values. This check examines if two string values are equal or not by using the "eq" and "ne" operators. We can also compare two strings with the help of the "lt, gt, ge, le" operators as well. In this tutorial, we will consider all the approaches that can be used to compare two strings in Perl. The "eq" and "ne" Operators in Perl Let's start with the eq and ... Read More

How to compare two arrays for equality in Perl?

Mukul Latiyan
Updated on 26-Dec-2022 16:44:26

2K+ Views

In Perl, there are two ways to check if two arrays are equal. We can compare two arrays in Perl with the help of "Storable freeze" function or by creating our own custom function. In this tutorial, we will explore both these approaches with the help of examples. Example 1 Let's first explore the "Storable freeze" code and understand how it works. Consider the code shown below. use Storable qw/freeze/; use strict; $Storable::canonical = 1; my @countriesOne = ('India', 'China', 'Russia', 'USA', 'Germany'); my @countriesTwo = ('India', 'China', 'Russia', 'USA', 'Germany'); my @countriesThree = ... Read More

How to check if a variable has a numeric value in Perl?

Mukul Latiyan
Updated on 26-Dec-2022 16:42:44

2K+ Views

Suppose we get a variable at runtime in Perl and we want to check if the value that it contains is numeric or not, then we can use the two approaches shown in this tutorial. We will use two simple examples to demonstrate how it works. Example  The most basic approach is to use the length and do keywords and then ignore the warnings. Consider the code shown below for the same. $x = 100; if (length(do { no warnings "numeric"; $x & "" })){ print "x is numeric"; } else { print ... Read More

How to check if a Perl hash already contains a key?

Mukul Latiyan
Updated on 26-Dec-2022 16:40:11

3K+ Views

Let's consider a scenario where we would want to know if a Perl hash already contains a key or not. To do so in Perl, we can use the exists() function. In this tutorial, we will explore the exists function with the help of two examples. The exists() Function in Perl In Perl, the exists() function checks whether a particular element exists or not in an array or a hash. If the requested element appears in the input array or hash, this function returns "1", else it returns "0". Example 1 Consider the code shown below. In this example, we ... Read More

How to check if a file exists in Perl?

Mukul Latiyan
Updated on 14-Mar-2023 18:05:09

2K+ Views

In this tutorial, we will take a couple of examples and demonstrate how you can if a file exists or not, with the help of Perl. Let's suppose we have a simple text file called "sample.txt" with the following data − This is a sample txt file that contains some content inside it. TutorialsPoint is simply amazing! We will use a Perl code to check whether this file exists or not. Example 1 The most basic approach to check whether a file exists or not is to use the "-e" flag and then pass the name of the file. ... Read More

How to check if a Perl array contains a particular value?

Mukul Latiyan
Updated on 26-Dec-2022 16:28:21

5K+ Views

In Perl, we can check whether an array contains a particular value or not with the help of the "grep" keyword. The grep function in Perl is used to filter the input supplied in the function as a parameter out of the list of items. Similar to Linux, it uses the given input to find the matching value. The grep() Method "grep" is a built-in function in Perl, we can pass our regular expression inside this function. It will check the input for matching values and return a list based on whether the condition is true or false. Syntax As ... Read More

1 2 3 4 5 ... 16 Next
Advertisements