Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

Re^3: How do I create a C-like struct with dynamic fields in Perl?

by perl-diddler (Hermit)
on Jul 19, 2012 at 04:27 UTC ( #982580=note: print w/ replies, xml ) Need Help??


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?


Comment on Re^3: How do I create a C-like struct with dynamic fields in Perl?
Select or Download Code
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

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://982580]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others browsing the Monastery: (6)
As of 2015-07-04 05:57 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (57 votes), past polls