Updated April 4, 2023
Introduction t0 Perl exists
As the name suggests exists function is used to check the existence of the element. In short, if we want to check whether the element present in the array or not, we can use this function to check it. This function is not limited to an array only by the use of it we can check hash elements also, so exists function can be used with both array and hash in Perl; also it is an inbuild function so easy to use we do not require any external library to use this. In the coming section, we will discuss more this function in detail.
Syntax
It is a function that takes a parameter to check the value in array and hash; we can pass our expression inside the function as an argument. Let’s see its syntax in detail for better understating to use this in programming see below;
exists(value_to_check)
In the above syntax, as you can see, we are passing a value inside this function as the parameter so that it will calculate the value against the hash or array; if the value presents, it will return 1 else 0. In the coming section, we will discuss more the method signature in detail.
How does exists function work in Perl?
We know that the existing function is used to check the element in the given array or hash in Perl. This function is important if we want to perform any specific operation in the presence of some specific values in an array or hash. Also, we can apply some conditions based on the result we obtained; this can be done accordingly. In this section, we will discuss more the signature of the exists function as per the Perl documentation. Also, we will discuss the return type and one sample example to understand its working in detail. Let’s start with the method signature see below;
Method signature
1. exists(value): By the use of this function, we can check a value inside the array or hash in Perl. As per its signature, it takes one parameter as the input. We can call this method on ant array or hash object in Perl. After this, we can pass the value which we want to check in the given array or hash.
2. return type: This function returns an integer value of 0 or 1 based on the result. If the value is found in the array or hash, it will return ‘1’; if the value is not present in the given array or hash, it will return ‘0’.
Now we will see one practice example of exists function how it works internally in Perl; to understand it better, see below;
my %myhash = (100 => 'Hello', 200 => 'bye', 300 => 'hello 3', 400 => 'hello 4');
print "calling function here " if exists $myhash{400};
As you can see in the above lines of code, we are using exists function in Perl to check the element in array and hash. But for this example, we are checking for a hash. Firstly, we have created one hash object and assign several values as key-value pairs. We have kept the key as the integer and value as String here like 100, 200, 300, 400, and so on. After this, we are calling the exists function on our hash object to check the value inside it. The value we want to check we have returned it inside the curly brackets ‘{}’ after this will check this value in hash and return us the result. In this way, we can check for elements in hash or array in Perl.
Points to remember while working with exists function in Perl;
1. As we know that it is an in-build function in Perl, so we do not need to include any library to use this function in Perl.
2. This function takes one argument as the input parameter, which will decide what value needs to be check in the given array or hash.
3. It will return an integer value 1 or 0 but not the value we searched for.
Examples
Here are the following examples mention below
Example #1
In this example, we are creating a hash object and assigned some values. After this, we are checking the values by using the exists function available in Perl. This is a simple example for beginners to start using it in practical uses.
Code:
my %myhash = (100 => 'Hello', 200 => 'bye', 300 => 'hello 3', 400 => 'hello 4', 500 => 'hello 5', 600 => 'hello 6', 700 => 'hello 7', 800 => 'hello 8');
print "Here we are calling exists method to check inside hash in Perl. \n";
print "checking for 100 \n" if exists $myhash{100};
print "checking for 200 \n" if exists $myhash{200};
print "checking for 400 \n" if exists $myhash{400};
print "checking for 600 \n" if exists $myhash{600};
print "checking for 800 \n" if exists $myhash{800};
Output:
Example #2
In this example, we are trying to print something by using if and else block to make the function more interactive. This would be more clear for the beginners to understand what is happening inside, how it is working, and what it is printing. Whether it is calculating correctly or not.
Code:
my %myhash = (100 => 'Hello', 200 => 'bye', 300 => 'hello 3', 400 => 'hello 4', 500 => 'hello 5', 600 => 'hello 6', 700 => 'hello 7', 800 => 'hello 8');
if(exists($myhash{600}))
{
print "Yes it found !! \n";
}
else
{
print "This does not exists in hash \n"
}
if(exists($myhash{200}))
{
print "Yes it found !! \n";
}
else
{
print "This does not exists in hash \n"
}
if(exists($myhash{1000}))
{
print "Yes it found !! \n";
}
else
{
print "This does not exists in hash \n"
}
if(exists($myhash{300}))
{
print "Yes it found !! \n";
}
else
{
print "This does not exists in hash \n"
}
if(exists($myhash{180}))
{
print "Yes it found !! \n";
}
else
{
print "This does not exists in hash \n"
}
Output:
Conclusion
So by the use of the exist function, we can check our element inside the array or hash in Perl. This function is very easy to use, readable and takes only one parameter. We can call this function on array or hash in Perl. By the use of this, we can check any element in the given input.
Recommended Articles
This is a guide to Perl exists. Here we discuss How does exists function work in Perl along with the codes and outputs. You may also look at the following articles to learn more –