Click here to Skip to main content
15,881,882 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i have to create two hashes say vegetable and fruit with key names as the product and values can be any (for vegetable:keys are carrot ,potato and for fruits keys are apple and mango. i have a particular product say apple .i should check particular product (i.e)apple belongs to which hash and print the respective hash in PERL.

What I have tried:

%fruit=>('apple',1,'mango',2,'grapes',3);
%vegetable=>('carrot',4,'potato',5);
$praticular_product="apple";
#check $praticular_product belongs to fruit hash or vegetable hash
Posted
Updated 24-Aug-20 20:02pm

You can use exists() function.
PERL
# Calling the exists() function 
if(exists($fruit{'apple'})) 
{ 
    print "Exists\n"; 
} 
else
{ 
    print "Not Exists\n"
}

Refer: Perl - Hashes - Tutorialspoint[^]
 
Share this answer
 
Comments
Garth J Lancaster 25-Aug-20 2:04am    
I often joke with OriginalGriff that there must be an invisible camera watching me post, because it's scary how much we post at the same time - ditto you Sandeep :)
Sandeep Mewara 25-Aug-20 2:09am    
:D. Have a 5.
learners0930 25-Aug-20 7:48am    
thank u so much Sandeep Mewara
For the first part, you could think about using the 'exists' Perl keyword, as in :

if (exists($fruit($praticular_product))
{
  print "Found Apple in fruit\n";
}
else
{
  print "Apple not found in fruit\n"
}


you can use 'for' to iterate (loop through) and then print a Perl hash - see here Perl Hash[^]
 
Share this answer
 
v2
Comments
Sandeep Mewara 25-Aug-20 2:09am    
+5 Ditto
learners0930 25-Aug-20 7:48am    
thank u so much Garth J Lancaster

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900