DBI fetchall_arratref, JSON and converting numeric "sting" to"number"

Discussion in 'Perl Misc' started by seven.reeds, Aug 8, 2009.

  1. seven.reeds

    seven.reeds Guest

    Hi,

    I am pulling data from some fairly simple database tables. I have a
    simple SELECT query for each table and I can do the fetchall_arrayref
    just fine. I want to format the returned data into a JSON string. I
    am doing this with only one problem. The data returned from the fetch
    is all "string"ified. If I have a table that has a numeric "id" field
    then the default JSON result has that column as a set of numbers in
    double quotes.

    I know that I can loop over the returned array and "add zero" to the
    numeric strings to have the JSON converter (JSON::Syck) treat them as
    numbers. This works but looking at the code makes me wonder if there
    is a more Perlesque way to do this. I've tried using map() on the
    arrayref but my understanding is too shallow to make that work yet.

    Ideas?
    seven.reeds, Aug 8, 2009
    #1
    1. Advertising

  2. seven.reeds

    seven.reeds Guest

    > Something like
    >
    >     use Scalar::Util qw/looks_like_number/;
    >
    >     for (@$results) {
    >         $_ += 0
    >             if looks_like_number $_;
    >     }


    Brilliant, thanks. Much more elegant
    seven.reeds, Aug 9, 2009
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. DeadInPlastic
    Replies:
    1
    Views:
    786
    Karl Heinz Buchegger
    Jun 26, 2003
  2. mark
    Replies:
    1
    Views:
    716
    Victor Bazarov
    Jun 19, 2004
  3. =?ISO-8859-1?Q?Gregory_Pi=F1ero?=

    How do I put % in a format sting?

    =?ISO-8859-1?Q?Gregory_Pi=F1ero?=, Oct 5, 2006, in forum: Python
    Replies:
    5
    Views:
    261
    John Salerno
    Oct 6, 2006
  4. darrel
    Replies:
    4
    Views:
    793
    darrel
    Jul 19, 2007
  5. Ivan Reborin
    Replies:
    27
    Views:
    1,661
Loading...

Share This Page