# Using JSON::XS use strict; use warnings; use DB_File; use JSON::XS qw( encode_json ); my %hash; unlink "tempfile"; #Remove previous file, if any tie %hash, "DB_File", "tempfile", O_RDWR | O_CREAT, 0666, $DB_HASH or die "Cannot open file 'tempfile': $!\n"; while ( $sourceString =~ /example(key)regex(value)example\b/ig ) { my $key = $1; my $value = $2; my $string_to_insert = encode_json( [ $key, $value ] ); push( @{ $hash{$key} }, $string_to_insert ); #Push the value into the hash } #And then use decode_json() from JSON::XS somewhere else to get an array of your values.