home PYTHONJAVA
 

Perl hash

A hash is a collection of key/value pairs.

The hash variable in Perl begins with a percent sign (%).

Access the hash element format: ${key}.

The following is a simple hash instance:

Instance

#!/usr/bin/perl %data = ( 'google', 'google.com', 'welookups', 'welookups.com', 'fb', 'fb.com'); print "\$data{'google'} = $data{'google'}\n"; print "\$data{'welookups'} = $data{'welookups'}\n"; print "\$data{'fb'} = $data{'fb'}\n";

Create a hash

Create a hash in two ways:

First, set value for each key

$data{'google '} = 'google.com';
$data{'welookups'} = 'welookups.com';
$data{'fb'} = 'fb.com';

Second, set by list

The first element in the list is key and the second is value.

%data = ('google' , 'google.com', 'welookups', 'welookups.com', ' fb', 'fb.com');

You can also use the => symbol to set key/value:

%data = ('google' =>'google.com', 'welookups'=>'welookups.com', 'fb'=>'fb.com');

The following example is a variant of the above example, using - instead of quotes:

%data = (-google= >'google.com', -welookups=>'welookups. Com', -fb=>'fb.com');

Using this method key can't have spaces, the way to read elements is:

$val = $data {-google}
$val = $data{-welookups}

Access hash element

Access the hash element format: ${key}, examples are as follows:

Instance

#!/usr/bin/perl %data = ( 'google'=>'google.com', 'welookups '=>'welookups.com', 'fb'=>'fb.com'); print "\$data{'google'} = $data{'google'}\n"; print "\$data{'welookups'} = $data{'welookups'}\n"; print "\$data{'fb'} = $data{'fb'}\n";

Read hash value

You can extract values ​​from the hash like an array.

Hash value is extracted into the array syntax format: @{key1,key2}.

Instance

#!/uer/bin/perl %data = ( -fb => 45, -google => 30, -welookups => 40); @array = @data{-fb, -welookups}; print "Array : @array\n";

Execute the above program, the output is:

pan class="hl-special">\n";
Array : 45 40

Read the hash's key and value

Read all keys

We can use the keys function to read all the keys of the hash. The syntax is as follows:

keys %HASH

This function returns an array of all the keys of all hashes.

Instance

#!/usr/bin/perl %data = ( 'google'=>'google.com', 'welookups '=>'welookups.com', 'fb'=>'fb.com'); @names = keys %data; print "$names[0]\n"; print "$names[1]\n"; print "$names[2]\n";

Execute the above program, the output is:

fb
Google
welookups

Similarly we can use the values function to read all the values ​​of the hash. The syntax is as follows:

values ​​%HASH

This function returns an array of all the values ​​of all hashes.

Instance

#!/usr/bin/perl %data = ( 'google'=>'google.com', 'welookups '=>'welookups.com', 'fb'=>'fb.com'); @urls = values %data; print "$urls[0]\n"; print "$urls[1]\n"; print "$urls[2]\n";

Execute the above program, the output is:

fb.com
welookups.com
Google.com

Check if the element exists

If you read a non-existing key/value pair in a hash, it will return a undefined value with a warning alert when executed.

To avoid this, we can use the exists function to determine if the key exists and read it when it exists:






welookups is optimized for learning.© welookups. 2018 - 2019 All Right Reserved and you agree to have read and accepted our term and condition.