Reputation: 50832
I have a perl variable like this. How can i access the inlying properties (like '706')?
my @config = [
{
'x' => [ 565, 706 ],
'y' => [ 122 ],
'z' => 34,
'za' => 59,
}
];
EDIT: print Dumper($config[0]);
yields : $VAR1 = undef;
Looks like i get acces using $config[0][0]->{x}[1];
. Why do i have to use [0][0] (one is clear, but he ssecond...)?
EDIT2: I am sorry for changing the data structure, but the definition which was given to me changed.
Upvotes: 1
Views: 110
Reputation: 91508
Your variable is equivalent to :
my $config = [
'x', [ 565, 706 ],
'y', [ 122 ],
'z', 34,
'za', 59,
];
So if you want to get the 706, you can do:
print $config->[1][1];
Updated according to new data in the question
With the updated question, you can access now by :
say $config->[0]{x}[1];
New update according to new data structure
According to the last updated data structure you provide:
my @config = [
{
'x' => [ 565, 706 ],
'y' => [ 122 ],
'z' => 34,
'za' => 59,
}
];
you assign an anonymous array [...] that contains itself a hash {...} to an array @config, this will populate the first element of @config with the anonymous array
say Dumper \@config;
$VAR1 = [
[
{
'y' => [
122
],
'za' => 59,
'x' => [
565,
706
],
'z' => 34
}
]
];
say $config[0][0]{x}[1]; #prints 706
I think you want to do either:
my $config = [
{
'x' => [ 565, 706 ],
'y' => [ 122 ],
'z' => 34,
'za' => 59,
}
];
say $config->[0]{x}[1]; #prints 706
my @config = (
{
'x' => [ 565, 706 ],
'y' => [ 122 ],
'z' => 34,
'za' => 59,
}
);
say $config[0]{x}[1]; #prints 706
Upvotes: 4
Reputation: 80433
[EDIT: Follow the shifting problem definition.]
Given:
my @config = (
[
{ # NB: insertion order ≠ traversal order
"x" => [ 565, 706 ],
"y" => [ 122 ],
"z" => 34,
"za" => 59,
},
],
);
Then this will do it:
# choice §1
print $config[0][0]{"x"}[-1]; # get 1ˢᵗ row’s xᵗʰ row’s last element
understanding of course that that is merely syntactic sugar for:
# choice §2
print $config[0]->[0]->{"x"}->[-1]; # get 1ˢᵗ row’s xᵗʰ row’s last element
and that that is just syntactic sugar for:
# choice §3
print ${ $config[0] }[0]->{"x"}->[-1]; # get 1ˢᵗ row’s xᵗʰ row’s last element
which in turn is just syntactic sugar for:
# choice §4
print ${ ${ $config[0] }[0] }{"x"}->[-1]; # get 1ˢᵗ row’s xᵗʰ row’s last element
which again is syntactic sugar for:
# choice §5
print ${ ${ ${ $config[0] }[0] }{"x"}}[-1]; # get 1ˢᵗ row’s xᵗʰ row’s last element
and that, of course, is equivalent to:
# choice §6
print ${ ${ ${ $config[0] }[0] }{"x"} }[ $#{ ${ ${ $config[0] }[0] }{"x"} } ]; # get 1ˢᵗ row’s xᵗʰ row’s last element
Upvotes: 3