How to split key value pair string to hash in Perl

Perl is highly advanced programming language in order to process with strings. In this article, I have explained about the simplest way to split the key value pair string to Perl hash.

Source code for split key value pair string to hash

use warnings;
use strict;
use Data::Dumper;

### Sample input
my $input = "key1=value1&key2=value2&key3=value3&key4=value4&key5=value5";

### Spliting
my %hash_response = split /[&=]/, $input;

print Dumper(\%hash_response);

Output

$VAR1 = {
'key1' => 'value1',
'key2' => 'value2',
'key3' => 'value3',
'key4' => 'value4',
'key5' => 'value5'
};

You Might Also Like

Leave a Reply

Your email address will not be published. Required fields are marked *