See docs https://metacpan.org/pod/JSON::PP#simple-scalars
You do print Dumper on hash, it stringifies all hash values, so you see such an obscure result for encode_json.
If don't dump $hash before encode json you get what you want - work_hours_to_float_try2 is json valid number
{"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"} 02.03.17 19:50, mailing lists via beginners пишет:
just to clarify, the real purpose of the script is output the hash to a json object, so the json output for the script: #!/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: beginners-unsubscr...@perl.org For additional commands, e-mail: beginners-h...@perl.org http://learn.perl.org/