Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

Re^6: Is there a way to make a JSON out of multiple records from MySQL?

by bartender1382 (Beadle)
on May 12, 2022 at 18:44 UTC ( [id://11143844]=note: print w/replies, xml ) Need Help??


in reply to Re^5: Is there a way to make a JSON out of multiple records from MySQL?
in thread Is there a way to make a JSON out of multiple records from MySQL?

Honestly, I got that. But I can't figure out which is uninitialized.

$arrayref is an array of hashfeferences, and $typehint_raws are empty, but existing arrays, one for each record.

And that's where's I'm stuck over what could be "uninitialized"

Is it possible that when I print Dumper $typehint_raws that I should be getting more than just?

.... $VAR1->[0], $VAR1->[0], $VAR1->[0], ...

After all, each record has three fields.

Replies are listed 'Best First'.
Re^7: Is there a way to make a JSON out of multiple records from MySQL?
by AnomalousMonk (Archbishop) on May 12, 2022 at 21:08 UTC
    .... $VAR1->[0], $VAR1->[0], $VAR1->[0], ...

    This means you are looking at copies of the same reference.

    Win8 Strawberry 5.8.9.5 (32) Thu 05/12/2022 17:00:05 C:\@Work\Perl\monks >perl use strict; use warnings; use Data::Dumper; my $array_ref = [ ([ qw(a b c) ]) x 4 ]; print "@$array_ref \n"; print Dumper $array_ref; ^Z ARRAY(0x983824) ARRAY(0x983824) ARRAY(0x983824) ARRAY(0x983824) $VAR1 = [ [ 'a', 'b', 'c' ], $VAR1->[0], $VAR1->[0], $VAR1->[0] ];
    A referent (a thing that can be pointed to) may have any number of references (things that point to it). There is one and only one referent.


    Give a man a fish:  <%-{-{-{-<

      I already solved my problem, posted about it. But wanted to further explain what you pointed out.

      I don't believe that I am looking at copies of the same reference

      sub makeJSONToExport { my ($arrayref) = @_; my $typehint_raw = { age => JSON_TYPE_INT, lastname => JSON_TYPE_STRING, firstname => JSON_TYPE_STRING}; my $typehint_raws = [ ($typehint_raw) x @$arrayref]; my $json = encode_json($arrayref, $typehint_raws); return $json; }

      I'm looking at $typehint_raws just before calling the encode_json command. So I believe I am seeing it just after it's initialized for the proper size, but before it's filled in by encode_json

        ... my $typehint_raws = [ ($typehint_raw) x @$arrayref]; my $json = encode_json($arrayref, $typehint_raws); return $json; ...
        I'm looking at $typehint_raws just before calling the encode_json command.

        If you're dumping $typehint_raws just before calling encode_json(), then you're dumping it just after initializing it (according to the posted code). In that case, the situation is just as given in the example here except that it's a hash reference that's being replicated rather than an array ref.

        I don't know what encode_json() does to "fill in" $typehint_raws, but it doesn't seem that it can be of much consequence since $typehint_raws goes out of scope with the return statement that immediately follows the subroutine call.


        Give a man a fish:  <%-{-{-{-<

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://11143844]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others rifling through the Monastery: (5)
As of 2024-03-28 13:35 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found