Gossamer Forum
Home : General : Perl Programming :

how to keep empty or space in the value?

Quote Reply
how to keep empty or space in the value?
Hi senior

I have been give a code to transfer sting to array of hash
it works however it could not include any empty or space or special symbol

for sample if i would like to have

desc2 => 'desc222 2'
desc2 => 'desc222-2'
desc2 => 'desc222 !*$2'
desc2 => ''
desc2 => 'Japanese_and_Chinese_fount'

it will return undef. would it be possible to correct it to have it able to include "space" "empty value" as well as Japanese fount or certain special symol ?

Thanks thanks




Code:
#!/usr/bin/perl
use strict;
use warnings;
use Data::Dumper;
#$str = "{file => 'file1', desc => 'test'}, {file => 'file12', desc => 'test2'}"
my $string = q({file => 'file1', desc => 'tedst', desc2 => 'desc222_2', desc3 => 'desc3_3'}, {file => 'file22', desc22 => 'desc22'});
my @array_of_hashes;

my @hashes = $string =~ /(\{.*?\})/g;
my @hashes = $string ;
for my $hashstr (@hashes) {
my @keys = $hashstr =~ /(\w+)\s*=>/g;




my @values = $hashstr =~ /=>\s*\'(\w+)\'/g;



my %hash;
@hash{@keys} = @values;
push @array_of_hashes, \%hash;
}
print "Pragma: no-cache\n";
print "Content-type:text/html\n\n";
print Dumper(\@array_of_hashes);

#Gives:
#$VAR1 = [
# {
# 'desc' => 'test',
# 'file' => 'file1'
# },
# {
# 'desc' => 'test2',
# 'file' => 'file12'
# }
#];
Quote Reply
Re: [courierb] how to keep empty or space in the value? In reply to
Finally i decide to use eval to turn sting into array of hash