in reply to Re^3: 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?

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

  • Comment on Re^4: How do I create a C-like struct with dynamic fields in Perl?

Replies are listed 'Best First'.
Re^5: How do I create a C-like struct with dynamic fields in Perl?
by Athanasius (Archbishop) on Jul 19, 2012 at 07:15 UTC
    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