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

I'm trying to store some data into a rather complex file structure and I'm having a little trouble with the syntax. I have two structs (one embedded within the other) that look like this:

struct Appt => { startTime => '$', endTime => '$', }; struct Day => { date => '$', appts => '@', numAppts => '$', };
You'll notice that the Day struct contains an array which I intend to be an array of Appt structs. I then have an array of Day structs (and a counter), like this:

my @days; my $dayCount = 0;
I seem to be able to add Day structs to my array, days, like this:

sub hdl_start { my ($p, $elt, %atts) = @_; if ( $elt eq 'DAY' ) { # Create a new Day struct my $temp = Day->new(); # Set the date element of that struct $temp->date($atts{"DATE"}); # Insert that struct into my array, days $days[$dayCount] = $temp; } $currEle = $elt; }
Unfortunately, I'm unable to add any elements to the appts array within the Day struct. This is the code I'm currently trying to use to add a new Appt struct to the appts array:

# Get the number of appointments my $numAp = $days[$dayCount]->numAppts; # Create a new Appt struct my $temp = Appt->new(); # Set the startTime element within my new Appt struct $temp->startTime("$str"); # Insert my new Appt struct into the appts array $days[$dayCount]->appts[$numAp] = $temp;
The error I am given looks like this: syntax error at line 81, near "->appts[". (Line 81 is the final line of code listed above.)

I'm assuming it's just a syntax error, but, unfortunately, I've been unable to track it down. I was hoping one of you could help.

Thanks,
-Sherlock

Replies are listed 'Best First'.
Re: Syntax Error with Embedded Structs
by princepawn (Parson) on Apr 18, 2001 at 00:50 UTC
    I'm not a big fan of Class::Struct... I prefer Class::MethodMaker. But oh well, I would have done this:
    # Insert my new Appt struct into the appts array push @{ $days[$dayCount]->appts }, $temp
    but I would make sure that
    ref($days[$dayCount]->appts) eq 'ARRAY';
    before doing so.

      'ARRAY' eq ref $x is a fragile construct. I think you (and more importantly, other people who want to use your code) will be better off if you use: UNIVERSAL::isa( $x, 'ARRAY' )

              - tye (but my friends call me "Tye")
Re: Syntax Error with Embedded Structs
by arturo (Vicar) on Apr 18, 2001 at 01:00 UTC

    I read this and I thought "Struct? Struct? What the heck is up with Structs in perl?" Then it hit me : you're using Class::Struct. That would be a good lead-in to the question =)

    It *seems like* overkill and retrograde non-Perlishness to me (I'll confess to not having any use for the module in question). Were it me, I'd use a home-grown data structure and avoid the problem (basic idea: each day would be an anonymous array of appointments, each of which is itself an anonymous array of start and end times; the whole shebang would be an array of days. Sounds complicated, but I think the syntax for dealing with it is less hairy. Still, you've got something different going on here)

    $days[$dayCount]->appts[$numAp]
    should probably be written
    $days[$dayCount]->appts($numAp) = $temp;

    Because, near as I can tell from reading the documentation, that there's a function call, not an array index.

    Update pfeh ... that won't work either, but for a different reason. I'd go with princepawn's suggestion and push a value onto that array. But the oddness of this all is what makes me long for a good old fashioned AoAoA =) (3d array ...)

    Philosophy can be made out of anything. Or less -- Jerry A. Fodor