sorting objects with "sort" and subroutine

J

Jay Eckles

I'm having some difficulty figuring out why a bit of code isn't
working. I'm hoping you can help.

I have a funciton that retuns an array of objects. I have defined the
class myself (the definition is below, it's quite short). I want to
sort that array, using a subroutine to define how the objects should
be sorted. Here's the line I use to call sort:

@events = sort calendar::timeCompare @events

Where calendar::timeCompare is the subroutine I've defined in a
package I called calendar.

The problem is that in timeCompare, I'm not able to access any of the
members of the objects $a and $b. For example,

package calendar ;
....
sub timeCompare{
print STDERR "a when: $a->{'when'}\n" ;
print STDERR "a summary: $a->{'summary'}\n" ;
....

gives me the following output:

a when:
a summary:

(I know that the object have values for 'when' and 'summary' because I
use the objects, after the sort call, and they have values).

Without access to these members, I can't sort the objects. Is there
any trick to sorting objects as opposed to scalar values?

Thanks for any assistance you can lend.

Jay Eckles

PS - here's the class definition:

###Event class
package Event ;

sub new {
my($class) = shift;

bless {
"date" => undef,
"when" => undef,
"location" => undef,
"summary" => undef,
"details" => undef
}, $class;
}

PPS - this is part of an open source project called CGI Calendar,
http://sourceforge.net/projects/cgicalendar
 
A

Andrew Tkachenko

Jay Eckles wrote on 27 ÐоÑбрь 2004 18:55:
I'm having some difficulty figuring out why a bit of code isn't
working. I'm hoping you can help.

I have a funciton that retuns an array of objects. I have defined the
class myself (the definition is below, it's quite short). I want to
sort that array, using a subroutine to define how the objects should
be sorted. Here's the line I use to call sort:

@events = sort calendar::timeCompare @events

Where calendar::timeCompare is the subroutine I've defined in a
package I called calendar.

The problem is that in timeCompare, I'm not able to access any of the
members of the objects $a and $b. For example,

package calendar ;
...
sub timeCompare{
print STDERR "a when: $a->{'when'}\n" ;
print STDERR "a summary: $a->{'summary'}\n" ;
...

gives me the following output:

a when:
a summary:

(I know that the object have values for 'when' and 'summary' because I
use the objects, after the sort call, and they have values).

Without access to these members, I can't sort the objects. Is there
any trick to sorting objects as opposed to scalar values?

Thanks for any assistance you can lend.

Jay Eckles

PS - here's the class definition:

###Event class
package Event ;

sub new {
my($class) = shift;

bless {
"date" => undef,
"when" => undef,
"location" => undef,
"summary" => undef,
"details" => undef
}, $class;
}

PPS - this is part of an open source project called CGI Calendar,
http://sourceforge.net/projects/cgicalendar

because $a, $ba variables produced by 'sort' are in global namespace.
Access them as $main::a and $main::b.
 
J

Jay Eckles

Andrew Tkachenko said:
because $a, $ba variables produced by 'sort' are in global namespace.
Access them as $main::a and $main::b.

That did the trick. Thanks!

Jay
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,764
Messages
2,569,564
Members
45,039
Latest member
CasimiraVa

Latest Threads

Top