sorting objects with "sort" and subroutine
From: Jay Eckles (j.eckles_at_computer.org)
Date: 11/27/04
- Next message: Andrew Tkachenko: "Re: sorting objects with "sort" and subroutine"
- Previous message: Nirvana: "500 Can't connect to search.cpan.org:80 (Bad protocol 'tcp') at web.pl line 24."
- Next in thread: Andrew Tkachenko: "Re: sorting objects with "sort" and subroutine"
- Reply: Andrew Tkachenko: "Re: sorting objects with "sort" and subroutine"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Date: 27 Nov 2004 10:55:56 -0800
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
- Next message: Andrew Tkachenko: "Re: sorting objects with "sort" and subroutine"
- Previous message: Nirvana: "500 Can't connect to search.cpan.org:80 (Bad protocol 'tcp') at web.pl line 24."
- Next in thread: Andrew Tkachenko: "Re: sorting objects with "sort" and subroutine"
- Reply: Andrew Tkachenko: "Re: sorting objects with "sort" and subroutine"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|
|