Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
PerlMonks  

Re: Get element of array not looping.

by choroba (Cardinal)
on Jul 13, 2021 at 17:59 UTC ( [id://11134969]=note: print w/replies, xml ) Need Help??


in reply to Get element of array not looping.

syntax error at ./script.pl line 8, near ""facts":"

Perl can parse JSON, but only from a string, not directly from the source code. In Perl hashes, keys are separated by commas or fat commas from the values, not colons. Then, you can do:

my $DATA = { facts => [ { name => 'A', type => 'Normal' }, { name => 'B', type => 'Broken' } ], }; print $DATA->{facts}[0]{name}; # A print $DATA->{facts}[0]{type}; # Normal

It's a hash containing an array containing hashes, not a hash of hashes of arrays.

Or, parse the JSON, but remove the trailing comma after the array, JSON doesn't support it:

#!/usr/bin/perl use warnings; use strict; use Cpanel::JSON::XS; my $DATA = decode_json('{ "facts": [ { "name": "A", "type": "Normal" }, { "name": "B", "type": "Broken" } ] }'); print $DATA->{facts}[0]{name}; # A print $DATA->{facts}[0]{type}; # Normal

map{substr$_->[0],$_->[1]||0,1}[\*||{},3],[[]],[ref qr-1,-,-1],[{}],[sub{}^*ARGV,3]

Replies are listed 'Best First'.
Re^2: Get element of array not looping.
by Anonymous Monk on Jul 13, 2021 at 18:58 UTC
    Perfect, thank you!!!

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others meditating upon the Monastery: (1)
As of 2024-04-24 15:08 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found