hhalpin has asked for the wisdom of the Perl Monks concerning the following question:

Sorry about first post!

I'm no perl pro - so _please_ help me with this question. How does one create a list of structs that contain a list of structs? Or even a struct that contains a list of structs? This code doesn't work and I don't know why:

use Class::Struct; struct DiscourseSegment => { intonation_step=>'$', speed_step=>'$', fluctuation_step=>'$', phrasenumber=>'$', #total number of phrases p => '@', #these should be of type Phrase }; struct Phrase => { text => '$', marking => '$', # 1 = SBEG, 2 = SF intonation => '$', #the level of intonation speed => '$', fluctuation => '$' }; $ds = (); $j = 0; $ds = DiscourseSegment->new(); $ds->p(["hey","you"]); @temp = $ds->p(0); print "*" x 30; $temp2 = $temp[0]; print "$temp2";
It won't print anything! I think it must be some syntactic error, but from the O'Reilly books I have it looks like it's correct. This shouldn't be too difficult :)I would also eventually want DiscourseSegment to be a list too.

Hhalpiin

P.S. - If perlmonks doesn't automatically do this, please e-mail me your response. Thanks! It will never print "hey you"

Replies are listed 'Best First'.
(crazyinsomniac) Re: How to use a list of structs that contain a list of structs?
by crazyinsomniac (Prior) on Sep 17, 2000 at 06:38 UTC
    On your code,

    The documentation says:

    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 => timeval, # seconds ru_stime => timeval, # microseconds }); struct( timeval => [ tv_secs => '$', tv_usecs => '$', ]);
    start_update:
    # create an object: my $t = new rusage; # $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);
    end_update:
    Notice the notation:
    struct(struct_name => {element => element_type});

    You're missing the parentheses.

    And as for emailing you our responses, an email address would be useful. The one you entered when you registered is kept private.

    As for adamsj's comment, I think a /msg should've sufficed.

     

    "cRaZy is co01, but sometimes cRaZy is cRaZy".
                                                          - crazyinsomniac
      Parenthesis don't matter here. struct is a function, and since Perl doesn't particularly care whether parenthesis exist in cases like this, his method will work just fine, and, IMO, is more readable. Think of => as a comma and pretend 'DiscourseSegment' is in quotes and things start to look normal from a syntax point of view.
Re: How to use a list of structs that contain a list of structs?
by Fastolfe (Vicar) on Sep 18, 2000 at 05:51 UTC
    I don't believe you can set the value of 'p' here in one call:
    $ds->p([ "hey", "you" ]); # Wrong $ds->p(0, "hey"); # Sets element 0 and $ds->p(1, "you"); # Sets element 1; OR: $ds = new DiscourseSegment ( p => [ "hey", "you" ] ); # Sets both at object creation time
    Note that I'm only saying it's wrong because it doesn't seem to be working for me either. The documentation for Class::Struct never seems to indicate that you can set @ types as you're attempting, though I would agree it is a logical way to do it. The docs only mention the two methods I describe above.
Re: How to use a list of structs that contain a list of structs?
by adamsj (Hermit) on Sep 17, 2000 at 01:08 UTC
    If you'll put your code inside a <CODE> block, you'll be much likelier to get a useful answer.

    Update: I see you did so--thank you!