Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine

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

by man-et-arms (Initiate)
on Jul 18, 2012 at 22:20 UTC ( #982549=perlquestion: print w/replies, xml ) Need Help??
man-et-arms has asked for the wisdom of the Perl Monks concerning the following question:

I want to create a C-like struct like this

struct { int data; struct *pointer; }

But the number of pointer fields will depend upon the info I'll get from a previously opened file.

I initially thought of doing so in C, but it seemed pretty impossible. So I thought of Perl, is there anyway I can do it, or at least simulate it? If so, how do I manipulate it?

Thanks in advance

Replies are listed 'Best First'.
Re: How do I create a C-like struct with dynamic fields in Perl?
by GrandFather (Sage) on Jul 19, 2012 at 01:45 UTC

    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

      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?

        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";};


        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
Re: How do I create a C-like struct with dynamic fields in Perl?
by tobyink (Abbot) on Jul 18, 2012 at 22:48 UTC

    Perhaps I'm missing something but it sounds like what you want are hash references.

    use 5.010; use strict; # Poor man's OO, using hash references. my $alice = { name => 'Alice' }; my $bob = { name => 'Bob' }; my $carol = { name => 'Carol' }; my $dave = { name => 'Dave' }; $alice->{spouse} = $bob; $bob->{spouse} = $alice; $carol->{spouse} = $dave; $dave->{spouse} = $carol; $bob->{employer} = $carol; say $alice->{name}; # Alice say $alice->{spouse}{name}; # Bob say $alice->{spouse}{employer}{name}; # Carol say $alice->{spouse}{employer}{spouse}{name}; # Dave
    perl -E'sub Monkey::do{say$_,for@_,do{($monkey=[caller(0)]->[3])=~s{::}{ }and$monkey}}"Monkey say"->Monkey::do'
      i'll try it, but i guess it does solve. thanks =)
Re: How do I create a C-like struct with dynamic fields in Perl?
by Marshall (Abbot) on Jul 19, 2012 at 06:55 UTC
    I initially thought of doing so in C, but it seemed pretty impossible
    I really doubt that proposition (This a common linked list problem in C).

    Perl can implement trees ("structs" that point to other structs) - no problem. This is often implemented as a reference to an anonymous hash.

    But often in Perl this is implemented as a simple "Array of Struct", which in Perl is often a AoH (array of hash references) and you get the appropriate result by sorting.

    If you are looking for a "simple solution", I would suggest that you give some data and what the desired output would be.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://982549]
Approved by tobyink
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others browsing the Monastery: (6)
As of 2018-02-22 21:45 GMT
Find Nodes?
    Voting Booth?
    When it is dark outside I am happiest to see ...

    Results (299 votes). Check out past polls.