I recommend to not try to directly translate between languages, as this likely results in a clumsy and unnatural code. That would certainly be the case here, as commented further down. The best I can do for this quest is to explain what the expression does
$ref -> { $struct2[$value]->{field1} }
-> { struct_insideStruct2 }
-> { $ref2->{field} }
The $ref
is a reference to a hash (associative array); it's OK to think of it as a pointer to a hash. One can tell because the ->
("arrow") operator dereferences, and the {...}
on its right means that on its left there must be a hash reference; this returns a value that it points to.
In this case, the key with which it is dereferenced (the index into the associative array) involves an element of the array @struct2
at index $value
; that element is another hash reference, being dereferenced (indexed into) with a key field1
(string literal†).
What this returns is another hash reference, which is then indexed into (dereferenced) with the key struct_insideStruct2
(string), and this again returns a hash reference.
That last one is indexed with a key which itself is produced by dereferencing another hash reference, $ref2
, with a key field
(string).
This is an example of a Perl complex data structure. How do you like it? I don't, not very much. Even in Perl, ideally I'd like to see this rewritten as a class, as it goes too deep and wide and so it packs too much complexity without any supporting structure which a class can provide.
If you still wish to indeed and really do that kinda thing in C, you can. May want to find a good hash implementation (or use struct
s and nest them carefully), and probably to dust off your function pointer syntax and such. But I would recommend to not get into all that.
Instead, once you understand the deep-nested data structure explained above, and the data it represents, find a way to implement what it means and does in your code in a native C way. We always want to use logic, techniques, and idioms native to the language at hand.
Along with linked documentation also see the short and sweet perlintro. The full reference for Perl's references is perlref.
† Normally such "barewords" need be under quotes, 'string'
(or using ""
, or q()
or qq()
operators ...). But if that is a sole thing between {}
then the quoting may be omitted.
$ref2->{field}
is a hash table lookup. So you need to create a hash table, see for example this page for an example of how to create one.$ref2->{field}
is a hash table lookup, but on a constant key. If you have only few keys, you might by able to use a struct, so that the C code would beref2->field
.