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.

smartmeta
  • 1,149
  • 1
  • 17
  • 38
Ash
  • 693
  • 1
  • 6
  • 9

1 Answers1

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);
HaloWebMaster
  • 905
  • 6
  • 16
  • I did try that, it works fine alone - but am not quite sure how to use that to create a nested structure. – Ash Jun 21 '12 at 20:25
  • I want something like ru_utime ='@Timeval', so that ru_utime would contain an array of 'Timeval' values. – Ash Jun 21 '12 at 20:32
  • 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]; – Ash Jun 22 '12 at 12:55
  • please clarify, that seems to be an array/aref to me – HaloWebMaster Jun 22 '12 at 12:58
  • 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... – Ash Jun 22 '12 at 13:04
  • Ah yes, that's what references are for. Use an array of object references: http://perldoc.perl.org/perldsc.html#REFERENCES – HaloWebMaster Jun 22 '12 at 13:07