Hashes as you know are basically key-value pairs. So in Perl, if you want to find the size of hashes, check this out.
Consider a simple practical real-time Perl script for storing stock quotes of various companies.
You see the %stock_quotes is the hash which has key value pairs i.e stock quote values in this case. Now, we store all the keys of the pairs in an array @keys. Note am not storing the values here, just the keys and we then find the size of this array @keys holding all the keys. Thus basically showing the size of the hash with $size_of_hash.
The resultant output is as expected :
Cheers!
Consider a simple practical real-time Perl script for storing stock quotes of various companies.
#!/usr/bin/perl
use strict;
use warnings;
my %stock_quotes;
my @keys;
my $size_of_hash;
%stock_quotes=(
Apple => 102,
Google => 720,
Microsoft => 53,
Yahoo => 34,
Walmart => 67
);
@keys = keys %stock_quotes;
$size_of_hash = @keys;
print "Stock hash size : $size_of_hash \n";
You see the %stock_quotes is the hash which has key value pairs i.e stock quote values in this case. Now, we store all the keys of the pairs in an array @keys. Note am not storing the values here, just the keys and we then find the size of this array @keys holding all the keys. Thus basically showing the size of the hash with $size_of_hash.
The resultant output is as expected :
Stock hash size : 5
Cheers!
No comments:
Post a Comment