http://www.perlmonks.org?node_id=982580


in reply to Re^2: How do I create a C-like struct with dynamic fields in Perl?
in thread How do I create a C-like struct with dynamic fields in Perl?

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?

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

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

      i was wishing to make ... many of those structs with an index for each.

      Here is one way, using anonymous hash references (see perlreftut):

      #! perl use strict; use warnings; my $dimension = 3; # change as needed my @array; push @array, { data => $_, pointers => [] } for (1 .. $dimension); push @{ $array[0]->{pointers} }, ('hello world', 'the end is nigh', 't +his is the end'); push @{ $array[1]->{pointers} }, ('so long', 'and thanks', 'for all th +e fish', 42); push @{ $array[2]->{pointers} }, (3.141592653589793, 'ciao!'); for my $struct (@array) { print "\nData is ", $struct->{data}, ".\n"; print 'There are ', scalar @{ $struct->{pointers} }, " pointers al +located:\n"; print "\t$_\n" for @{ $struct->{pointers} }; }

      See also: perldsc and perlref.

      If your structs become too complex, it may be better to implement them as objects — see perlootut.

      HTH,

      Athanasius <°(((><contra mundum