Skip to content

Instantly share code, notes, and snippets.

@rozer007
Created December 6, 2021 11:05
Show Gist options
  • Save rozer007/097a90f37bce688778eebddcfe445cfe to your computer and use it in GitHub Desktop.
Save rozer007/097a90f37bce688778eebddcfe445cfe to your computer and use it in GitHub Desktop.
Hashes in Perl
Q1. What symbol is use to represent the hashes in perl?
ans: The % symbol is use to represent hashes in perl.
Q2.How can we convert a hashes to an array?
ans: We can convert a hashes to an array using this syntax: my @var = %hashes.
Q3.How can we access item from a hash in perl?
ans: We will use sigil $ while accessing the item in hash. syntax: hash
Q4.What function is use to check if a key in present in a hash?
ans We use exists operator to check whether a key is present or not.
Q1.What will be the output of this statement:
my %hash=("a"=>96,
"b"=>98,
"c"=>99);
print $hash{"c"}++;
a) 99
b) 100
c) error
d) c=>99
ans: a)
Q2.What will be the output:
my %hash=("a"=>96,"b"=>98,"c"=>99);
my @var=%hash;
print @var[2];
a) b
b) a
c) error
d) not able to defined.
ans: b)
Q3.What will this statement give:
my %hash=("a"=>96,"b"=>98,"c"=>99);
my $k=keys %hash;
print $k;
a) all the keys
b) The fisrt key in the hash
c) the length of the hash
d) the number of keys in hash
ans: d)
Q4)What will be the output of the statement:
my %hash=("a"=>96,"b"=>98,"c"=>99);
while(my($k,$v)=each(%hash))
{
$hash{$k}++;
};
my @arr=@hash{'a','b','c'};
print @arr;
a) 96,97,98,99
b) 969899
c) 97,99,100
d) 9799100
ans: d)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment