in reply to Re: String to Integer, for json file
in thread String to Integer, for json file

Hi Karl, I haven't installed JSON::XS yet (don't want to install dependencies yet) - could you try this out?

my %hash; $hash{'cl_ip'} = "29988" + 0;
I think the value is treated as a string at some point, perhaps when I search and replace any non-digit character. Thanks, Rachel

Replies are listed 'Best First'.
Re^3: String to Integer, for json file
by karlgoethebier (Abbot) on Dec 12, 2013 at 20:03 UTC
    "...could you try this out?"

    Why not, nice invitation:

    karls-mac-mini:monks karl$ perl -MData::Dumper -e '$hash{'cl_ip'} = "2 +9988" + 0;print Dumper(\%hash);'$VAR1 = { 'cl_ip' => 29988 };

    But: so what ;-)

    Update:

    D'oh! Mille Regretz!

    #!/usr/bin/env perl + use JSON::XS; use Data::Dumper; my %hash = ( cl_ip => 209890 ); my $json = encode_json \%hash; print Dumper( \%hash ); print qq($json\n); $hash{ cl_ip } += 0; $json = encode_json \%hash; print Dumper( \%hash ); print qq($json\n); __END__ karl-mac-mini:monks karl$ ./json.pl + $VAR1 = { + 'cl_ip' => 209890 + }; + {"cl_ip":209890} + $VAR1 = { + 'cl_ip' => 209890 + }; + {"cl_ip":209890}

    Update2:

    #!/usr/bin/env perl + use JSON::XS; use Data::Dumper; my %hash = ( cl_ip => 209890 ); my $json = encode_json \%hash; print Dumper( \%hash ); print qq($json\n); $hash{ cl_ip } += 0; $json = encode_json \%hash; print Dumper( \%hash ); print qq($json\n); $hash{ cl_ip } = "209890"; $json = encode_json \%hash; print Dumper( \%hash ); print qq($json\n); __END__ $VAR1 = { 'cl_ip' => 209890 }; {"cl_ip":209890} $VAR1 = { 'cl_ip' => 209890 }; {"cl_ip":209890} $VAR1 = { 'cl_ip' => '209890' }; {"cl_ip":"209890"}

    I'll give it up for tonight before i produce more shit...

    Update3:

    I'll give it up...

    I don't:

    #!/usr/bin/env perl use JSON::XS; use Data::Dumper; use strict; use warnings; my %hash = ( cl_ip => 209890 ); print Dumper( \%hash ); print qq(\n); my $json = encode_json \%hash; print qq($json\n\n); #------------------------------ $hash{cl_ip} += 0; print Dumper( \%hash ); print qq(\n); $json = encode_json \%hash; print qq($json\n\n); #------------------------------ $hash{cl_ip} = "209890"; print Dumper( \%hash ); print qq(\n); $hash{cl_ip} += 0; print Dumper( \%hash ); print qq(\n); $json = encode_json \%hash; print qq($json\n\n); __END__ karl-mac-mini:monks karl$ ./json.pl $VAR1 = { 'cl_ip' => 209890 }; {"cl_ip":209890} $VAR1 = { 'cl_ip' => 209890 }; {"cl_ip":209890} $VAR1 = { 'cl_ip' => '209890' }; $VAR1 = { 'cl_ip' => 209890 }; {"cl_ip":209890}

    My best regards, Karl

    «The Crux of the Biscuit is the Apostrophe»