in reply to How do I create a C-like struct with dynamic fields in Perl?

Where you would think "struct" in Cish languages think Hash in Perl. Where you would think "pointer to array" (to get an array of arbitrary size) in C, think Array in Perl.

Perl hashes are associative arrays where a scalar value is accessed using a string. A scalar value can be a reference to something else (like an array, a hash, a subroutine, ...) so you can build nested data structures in whatever way suits the problem at hand.

Perl arrays are dynamically sized so you can grow and shrink them as suits your application. In fact you can edit them (insert or delete blocks of elements) in pretty much any way you like and not worry about complicated book keeping or large computation costs.

In Perl an instance of your struct and its use would look something like:

use strict; use warnings; my %struct = (data => 1, pointers => []); print "Data is $struct{data}.\n"; print "There are ", scalar @{$struct{pointers}}, " pointers allocated. +\n"; push @{$struct{pointers}}, "hello world", "the end is neigh", "this is + the end"; print "$_\n" for @{$struct{pointers}}; print "There are ", scalar @{$struct{pointers}}, " pointers allocated. +\n";

which prints:

Data is 1. There are 0 pointers allocated. hello world the end is neigh this is the end There are 3 pointers allocated.
True laziness is hard work

Replies are listed 'Best First'.
Re^2: How do I create a C-like struct with dynamic fields in Perl?
by man-et-arms (Initiate) on Jul 19, 2012 at 04:01 UTC

    wow that's really amazing \o/. but how could i make an array of this struct?

      Make an array with the struct? Like:
      my $struct= {pointers=>[]};
      You don't really need a count of them, as you can use $# or @:
      $#{$struct->{pointers}} = last element index of pointers<br> @{$struct->{pointers}} = array
      But then...if all you are storing is the array, you could just use it with no hash.
      $ptrs=[]; push @{$ptrs}=$newpointer; $#{$ptrs}; last index @{$ptrs}; array itself
      Or since you aren't using an index, both $#$ptrs and @$ptrs will work as well.

      or did you mean something else?

        i was wishing to make and array of the hash GrandFather posted. i mean, getting many of those structs with an index for each.

      You'd need to take a reference (a hashref) to the %struct hash. E.g.

      my %struct = (data => 1, pointers => []); my $struct_ref = \%struct;

      Or, in a single step:

      my $struct_ref = {data => 1, pointers => []};

      Note the reference is a SCALAR value (uses $ sigil). There's nothing special about using these in an array:

      my @array_of_structs = (\%struct1, $struct2_ref, {data => 1, pointers +=> []}); push @array_of_structs, \%new_struct;

      Access the hash data like this:

      my $first_data = $array_of_structs[0]{data}; push @{ $array_of_structs[2]{pointers} }, 'abc', 'def'; my $third_struct_second_pointer = $array_of_structs[2]{pointers}[1];

      -- Ken

      At this point it would probably help to know a little more about the problem you are trying to solve, but the following example may be relevant.

      use strict; use warnings; my @structs; local $/ = '};'; while (my $struct = <DATA>) { my ($name, $data) = $struct =~ m/\bstruct\s+(\w+).+\bint\s*:\s*(\d ++)\s*;/s; my @strings = $struct =~ m/(?<={|;)\s*pointer:\s*"([^"]*)";/sg; next if ! defined $data || ! @strings; push @structs, {name => $name, data => $data, strings => \@strings +}; } for my $struct (@structs) { print <<STRUCT; struct $struct->{name} { int: $struct->{data}; STRUCT print " pointer: $_;\n" for @{$struct->{strings}}; print "};\n\n" } __DATA__ struct first { pointer: "hello world"; pointer: "the end is nigh"; pointer: "this is the end"; int: 23; }; struct second { pointer: "Gidday"; int: 12; pointer: "the second struct is nigh"; pointer: "in fact this is it"; }; struct third {int: 21; pointer: "When two is not enough, you need a th +ird";};

      Prints:

      struct first { int: 23; pointer: hello world; pointer: the end is nigh; pointer: this is the end; }; struct second { int: 12; pointer: Gidday; pointer: the second struct is nigh; pointer: in fact this is it; }; struct third { int: 21; pointer: When two is not enough, you need a third; };
      True laziness is hard work