Perl nested structures -
I have a question about complex structures in Perl
My $ data1 = [ + {ID = & gt; 1, name = & gt; 'A'}, + {id = & gt; 2, name = & gt; 'B'}, + {id = & gt; 3, name = & gt; 'C' }, ]; My $ data3 = + {1 = & gt; + {ID = & gt; 1, name = & gt; 'A'}, 2 = & gt; + {ID = & gt; 2, name = & gt; 'B'}, 3 = & gt; + {ID = & gt; 3, name = & gt; 'C' }, }; How should I print "B"? What kind of data structure is that?
Thank you in advance
Try doing this:
print $ data1-> [1] - & gt; {Name}; # ARRAY ref print $ data3- & gt; {2} - & gt; {Name}; #hash referee This will be a perl ARRAY and HASH Ref from de-reference . - & gt; D-context This is explicitly required only for the first "floor", ex: print $ data1-> [1] { Name}; Print $ data3- & gt; {2} {name}; Also there are 2 more options that works. Like Chris Charlie, take a look at
to help you understand how your scalar ref looks, use, eg:
Print dumper $ data1; Print dumper $ data3; should output:
$ VAR1 = [{'name' = & gt; 'A', 'id' = & gt; 1}, {'name' = & gt; 'B', 'ID' = & gt; 2}, {'name' = & gt; 'C', 'ID' = & gt; 3}]; $ VAR1 = {'1' = & gt; {'Name' = & gt; 'A', 'id' = & gt; 1}, '3' = & gt; {'Name' = & gt; 'C', 'ID' = & gt; 3}, '2' = & gt; {'Name' = & gt; 'B', 'ID' = & gt; 2}};
+ {} , gives a good response to the syntax: Disables where those places suspend a Anonymous hash should be a code block instead of context, but one rarely needs it. I'm guessing that the code has a wrong desire to be clear and consistent with those places, where it is unclear.
Comments
Post a Comment