in reply to Re: Adjusting variable context when working with JSON data?
in thread Adjusting variable context when workign with JSON data?

Oops, I think I accidentally removed that closing curly brace for the loop there. I originally posted the without using any HTML tags and went back to adjust the post manually... got a little too carried away.

You are probably right, spawning new processes is always expensive, and making this only once shows that Perl runs much faster. I would like to play with hyperfine as suggested above, so I might try that when I have something a bit more meaningful to test on.

The "benchmark" was just for fun, thought, to see how Python and Perl compare. What I'm more concerned is with the correctness of the script, specifically how the JSON is processed. As I said, I find this syntax a bit strange: (@$decoded). I couldn't figure it out without looking for answers online and I'm not sure if there's perhaps a better way to tell Perl that this is an array (assuming that I know in advance the structure of the JSON to be returned)?

Replies are listed 'Best First'.
Re^3: Adjusting variable context when working with JSON data?
by choroba (Cardinal) on Dec 03, 2024 at 21:38 UTC
    You can start with an array at the beginning:
    my @decoded = @{ decode_json($result) };
    map{substr$_->[0],$_->[1]||0,1}[\*||{},3],[[]],[ref qr-1,-,-1],[{}],[sub{}^*ARGV,3]

      I like that you can do this, duly noted. Thanks!

Re^3: Adjusting variable context when working with JSON data?
by Danny (Chaplain) on Dec 05, 2024 at 00:57 UTC
      I find this syntax a bit strange: (@$decoded)

    That's something you will have to get used to as it's a fundamental syntax in perl. $decoded is a reference to an array. To dereference it you just put a @ at the front. e.g. $x = [1,2,3]; @array = @$x. You can also put squiggles around the reference if that makes it clearer and in some cases this is needed, like @{$decoded}.

      I thought that you "\" to de-reference, or maybe that's just for hashes? But in any case I think you're right, I need to write some more Perl to get used to this. I've only started to pick it up out of curiosity, and so far I've only written a few things here and there to get a taste of the basics. Thanks for the help!

        Preceding with a backslash creates a reference to the operand. De-referencing is the opposite of this. Do have a read of the intro to references tutorial, perlreftut and perlref for getting started with references.


        🦛