Home: Need a Custom or Prewritten Perl Program?: I need a program that...:
Create a hash from a string



hujirong
Novice

Jun 25, 2015, 8:47 AM


Views: 12400
Create a hash from a string

# Hash my %weekly_temperature = ('monday', 65, 'tuesday', 68, 'wednesday', 71, 'thursday', 53, 'friday', 60);

Using the above sample, I now have a string can be formatted like this:
"'monday', 65, 'tuesday', 68, 'wednesday', 71, 'thursday', 53, 'friday', 60". How can I create a hashfrom this string? or I can format the string in whatever way it takes. It's transferred from another system.

Thanks
Jirong


(This post was edited by hujirong on Jun 25, 2015, 8:48 AM)


Laurent_R
Veteran / Moderator

Jun 27, 2015, 4:51 AM


Views: 12382
Re: [hujirong] Create a hash from a string

Perhaps something like this:


Code
my $string = "'monday', 65, 'tuesday', 68, 'wednesday', 71, 'thursday', 53, 'friday', 60"; 
my %weekly_temp = split /[,\s]+/, $string;