0
use Class::Struct;

struct(TradeId => [
        tradeIdScheme => '$',
        id => '$',
]);

struct(VersionedTradeId => [
        tradeId => 'TradeId',
        version => '$',
        effectiveDate => '@',
]);

I would like to create an array of 'TradeId' inside the 'VersionedTradeId' structure.

1 Answer 1

2

Try this: http://www.nntp.perl.org/group/perl.beginners/2007/09/msg95440.html

See Example 1 from Class::Struct docs:

Example 1

Giving a struct element a class type that is also a struct is how structs are nested. Here, Timeval represents a time (seconds and microseconds), and Rusage has two elements, each of which is of type Timeval .

use Class::Struct;
struct( Rusage => {
ru_utime => '@', # user time used
ru_stime => 'Timeval', # system time used
});
struct( Timeval => [
tv_secs => '$', # seconds
tv_usecs => '$', # microseconds
]);
# create an object:
my $t = Rusage->new(ru_utime=>[Timeval->new(), Timeval->new(), Timeval->new()], ru_stime=>Timeval->new());
# $t->ru_utime and $t->ru_stime are objects of type Timeval.
# set $t->ru_utime to 100.0 sec and $t->ru_stime to 5.0 sec.
$t->ru_utime->tv_secs(100);
$t->ru_utime->tv_usecs(0);
$t->ru_stime->tv_secs(5);
$t->ru_stime->tv_usecs(0);
Sign up to request clarification or add additional context in comments.

6 Comments

I did try that, it works fine alone - but am not quite sure how to use that to create a nested structure.
I want something like ru_utime ='@Timeval', so that ru_utime would contain an array of 'Timeval' values.
It does work, but I don't want ru_utime to be an array. Rather I am looking for an array of ru_utime. Something similar in C would be like struct books { int bookid;} then creating something like struct books book[10];
please clarify, that seems to be an array/aref to me
How about struct books { int bookid; char bookname[20];} and then creating struct library_item { struct books book[10]; struct cds cd[10];} As you can see I need 10 structures of the type book to be created rather than 10 booknames inside the structure books. I hope you understand what am asking...
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.