2

I've started working on a program which is in Perl and has to be transformed into C.

There are a lot of subroutines which have structure member accessing which is unfamiliar to me, because I have little to no knowledge about Perl syntax and structure flow. Example:

$ref->{$struct2[$value]->{field1}}->{struct_insideStruct2}->{$ref2->{field}}

$ref is a third structure

$ref2 is a local copy of a parameter which is of type struct1

My question is: How do you create a line like this in C?

Do I need to create nested multiple structures?

I need to understand how multiple access operators in Perl works and if I can create something similiar in C, thanks in advance.

8
  • 1
    $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. Nov 9, 2020 at 14:43
  • Are you really sure you want to write that in C? It is hellacious to emulate that. Perl will probably do it faster; it has had decades of experts optimizing it. Review what you have to transform that code into C. Then, I strongly recommend you decide that it does not need to be transformed into C. Nov 9, 2020 at 15:14
  • I know, but sadly I do need to do it in C, or at least make it similar. But the previous comment by Hakon gives me an idea, that I might need a hash table for this problem. I agree for the part about Perl, it's just the task requiring it, otherwise I would not do it
    – Gadr1
    Nov 9, 2020 at 15:37
  • 2
    It's hard to judge what a C implementation would look like from just one line given out of context. $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 be ref2->field.
    – M Oehm
    Nov 9, 2020 at 17:05
  • Instead of replicating the Perl logic, figure out the best way to represent your data in the C program. From that, use whatever access techniques make sense for the problem. Or, embed a perl interpreter in your C program. Nov 10, 2020 at 6:20

1 Answer 1

2

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 structs 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.

3
  • @Gadr1 I hope this helps, even as it doesn't give you an equivalent C code. That'd be a bit of a grand undertaking, which I recommend against (along with other poeple) -- and which we couldn't do without knowing what that program does. Please let me know if more clarification can help
    – zdim
    Nov 9, 2020 at 20:30
  • I decided to go for Hash table, because I need it only for an if statement to find a record and check its value. It allowed me to search for that record and check out the value for a function it needed. Thanks for the comments and help guys, since it's my first question on stackoverflow
    – Gadr1
    Nov 11, 2020 at 9:22
  • @Gadr1 Great, that's one way I meant it -- find a hash implementation (is that what you mean?), and more to the point understand your data and work with it so to not have to blindly "translate." It's a good question and how you posed it I think, it's just that the whole proposition itself difficult.
    – zdim
    Nov 11, 2020 at 20:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.