On Thu, Mar 02, 2017 at 04:50:32PM +0000, mailing lists via beginners wrote: > > > just to clarify, the real purpose of the script is output the hash to a json > object, so the json output for the script:
So you have to "numify" your number: https://metacpan.org/pod/JSON::PP#PERL-%3E-JSON > #!/usr/bin/perl > use strict; > use warnings; > use Data::Dumper; > use JSON::PP; > > my ($input_string, %job_task); > > sub sanitize_data{ > my $data = shift; > > $data->{'work_hours'} = (split(/\s+/,$data->{'work_hours'}))[0]; > $data->{'work_hours_to_float_try1'} = sprintf("%.2f",$data->{'work_hours'}); > $data->{'work_hours_to_float_try2'} = 0 + $data->{'work_hours'}; > $data->{'work_hours_to_float_try3'} = 1.1 + 4.1; > > print Dumper $data; > my $json_text = encode_json($data); > print $json_text,"\n"; > }; > > $input_string = ' Work Hours: 4.1 hours'; > > if($input_string =~ /^\s+([^:]+):\s+(.*)/){ > my ($k, $v) = ( lc($1), $2 ); > $k =~ s/\s/_/g; > $job_task{$k}=$v; > sanitize_data(\%job_task); > }; > > > > returns: > > $VAR1 = { > 'work_hours_to_float_try3' => '5.2', > 'work_hours_to_float_try2' => '4.1', > 'work_hours' => '4.1', > 'work_hours_to_float_try1' => '4.10' > }; > {"work_hours_to_float_try3":"5.2","work_hours_to_float_try2":"4.1","work_hours":"4.1","work_hours_to_float_try1":"4.10"} > > > As you can see, the values are strings (note the double quotes around the > numeric values) and not floats. So the real question is how to output float > numbers in this case. > > -- > To unsubscribe, e-mail: [email protected] > For additional commands, e-mail: [email protected] > http://learn.perl.org/ > > -- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected] http://learn.perl.org/
