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

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

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

Replies are listed 'Best First'.
Re^3: How do I create a C-like struct with dynamic fields in Perl?
by perl-diddler (Chaplain) on Jul 19, 2012 at 04:27 UTC
    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.

        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

Re^3: How do I create a C-like struct with dynamic fields in Perl?
by kcott (Archbishop) on Jul 19, 2012 at 07:04 UTC

    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

Re^3: How do I create a C-like struct with dynamic fields in Perl?
by GrandFather (Saint) on Jul 19, 2012 at 10:51 UTC

    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