Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
PerlMonks  

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 ( [id://11143852]=note: print w/replies, xml ) Need Help??


in reply to Re^6: 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?

.... $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:  <%-{-{-{-<

Replies are listed 'Best First'.
Re^8: Is there a way to make a JSON out of multiple records from MySQL?
by bartender1382 (Beadle) on May 12, 2022 at 23:14 UTC

    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:  <%-{-{-{-<

        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.

        Not sure why you say that. I've run the code dozens of times, on multiple tables and searches. Not just getting back the proper output, but my server error log shows no errors.

        still asking because I don't want to get bit in the rear later on.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others meditating upon the Monastery: (4)
As of 2024-03-29 07:54 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found