Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation

Re: Parsing data from JSON response file.

by tangent (Vicar)
on Apr 11, 2020 at 04:02 UTC ( #11115362=note: print w/replies, xml ) Need Help??

in reply to Parsing data from JSON response file.

If the data is as you show in the example then you do not need the loop, you can just access whatever you need directly. To see what is happening within your loop, add another print statement:
for my $key ( keys %$data ) { print "key: $key\n"; ...

Replies are listed 'Best First'.
Re^2: Parsing data from JSON response file.
by Marshall (Canon) on Apr 13, 2020 at 03:12 UTC
    Good idea. That is actually one of the first things I did when running the code. And then came the realization that $key wasn't used anywhere within the loop!
    for my $key ( keys %$data ) { print "key = $key\n"; #debug my $zip = $data->{'zip'}; my $descri = $data->{'happen'}[0]->{'descri'}; my $extra_name = $data->{'notas'}[0]->{'ExtraName'}; print "\n $zip - $descri - $extra_name\n"; }
    I guess this is "off topic", but a feature of Perl.

    If you have a constant like: use constant DEBUG => 1; and then have a statement like print "whatever line(s)" if DEBUG;, Perl is smart enough to eliminate this statement from the executable code when DEBUG is false (set to zero). For complex modules, I often leave code like that in the source. When I add a new feature, I turn debugging on. Print is your friend. I seldom need the Perl debugger.


      That's easy enough to verify with B::Concise:

      $ cat use strict; use warnings; use constant DEBUG=>0; print "Foo bar!\n"; print "Baz\n" if DEBUG; $ perl -MO=Concise,-exec 1 <0> enter 2 <;> nextstate(main 191 v:*,&,{,x*,x&,x$,$ 3 <0> pushmark s 4 <$> const[PV "Foo bar!\n"] s 5 <@> print vK 6 <;> nextstate(main 191 v:*,&,{,x*,x&,x$,$ 7 <@> leave[1 ref] vKP/REFC syntax OK

      Yep, it's stripped out!


      When your only tool is a hammer, all problems look like your thumb.

Log In?

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

How do I use this? | Other CB clients
Other Users?
Others exploiting the Monastery: (5)
As of 2022-01-18 16:51 GMT
Find Nodes?
    Voting Booth?
    In 2022, my preferred method to securely store passwords is:

    Results (53 votes). Check out past polls.