I have a problem accessing to data, described in the title of the question.
The structure of the data is the following:
my @structure = (
{
"Name" => "Test",
"Param1" => 1,
"Data1" => \@test1,
"Data2" => [\@test3],
"Data3" => [\@test1, \@test2],
},
...
);
And I need to access the 1st (#0) element of the array @test3.
I tried something like this:
my @array = @{$_->{'Data2'}}[0];
print $array[0];
But then I get a reference to the array (ARRAY(0x239c3c)) instead of the required meaning. I feel that there’s something global that I don’t understand here.
Couldn’t you explain how and why I should address to the required meaning?
Thanks a lot.
You can use the syntactic sugar of
->to get the reference you want. It’s much cleaner than using parentheses.Okay, in your structure, it’d be:
This is equivalent, but harder to read:
And, in this particular case, you can eliminate the syntactic sugar
->:However, I don’t know if that makes it easier to read, and there are cases where the default way of parsing that might not do exactly what you want. I tend to squeeze everything together when I’m referring to an array of arrays, but that’s it:
Otherwise, I’ll use the syntactic sweetener of
->.Here’s an explanation of the syntax:
$structure[0]is a reference to the first item of your@structurearray. This contains a reference to a hash.$structure[0]->{Data2}is a reference to the hash elementData2in the first item in your@structurearray. This is a reference to an array.$structure[0]->{Data2}->[0]is a reference to the first array reference in theData2hash element in the first element (0) of your@structurearray. This is your@test3array.$structure[0]->{Data2}->[0]->[0]is the first element of the@test3array.I also recommend you use the
Data::Dumpermodule as you write your code. It’ll help you see what structure you’re referencing and help point out any errors in the levels. For example:Will show you that this is a reference to another array layer.
Will show you this is a reference to a hash where each item contains references to arrays of arrays.
Whenever you use these highly complex structures, it screams for using object oriented Perl coding. I’m not 100% sure what you’re doing, so I can’t help you with your objects, but highly complex structures tend to break and are almost impossible to maintain.
For example, we now do this:
This quickly detects that I misspelled my variable name by doing
$fooinstead of$Foo. This was a big improvement over older versions of Perl where this type of error checking couldn’t be done.However, now look at this:
We’ve lost the compilation error checking.
%hashis a legitimate variable, but$hash{Foo}is defined and not$hash{foo}. In this case,use strictdoes nothing for you.Using object oriented Perl returns this error check:
Whoops! The method should be
Fooand notfoo. However, Perl is able to detect that the methodfoodoesn’t exist and will once again generate an error on compile.In the above case, it’s pretty straight forward, but you’re dealing with an array of hashes of arrays of arrays. The possibility of an error is multiplied by 100 with each reference access. And, because you have to spell out these references over and over in your code, an error can be located in dozens of different places. Even if you could code this initially and then comment the entire structure in your code, it’ll be impossible to maintain this when you come back to this after three months.
Take a look at Perldoc’s Tutorials and go through the ones for object oriented programming.