sorting objects with "sort" and subroutine

Discussion in 'Perl Misc' started by Jay Eckles, Nov 27, 2004.

  1. Jay Eckles

    Jay Eckles Guest

    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
    Jay Eckles, Nov 27, 2004
    #1
    1. Advertising

  2. 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.


    --
    Andrew
    Andrew Tkachenko, Nov 27, 2004
    #2
    1. Advertising

  3. Jay Eckles

    Jay Eckles Guest

    Andrew Tkachenko <> wrote in message news:<coaj5o$jnl$>...
    > 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
    Jay Eckles, Nov 27, 2004
    #3
  4. Jay Eckles

    Guest

    test...
    , Dec 2, 2004
    #4
  5. <> wrote:

    > test...



    *plonk*


    --
    Tad McClellan SGML consulting
    Perl programming
    Fort Worth, Texas
    Tad McClellan, Dec 2, 2004
    #5
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Casey
    Replies:
    3
    Views:
    851
    Casey
    Jan 30, 2004
  2. Replies:
    7
    Views:
    740
    Stefan Arentz
    Sep 10, 2007
  3. Navin
    Replies:
    1
    Views:
    690
    Ken Schaefer
    Sep 9, 2003
  4. Nath

    Sort array - subroutine help

    Nath, Aug 16, 2004, in forum: Perl Misc
    Replies:
    4
    Views:
    102
    Uri Guttman
    Aug 16, 2004
  5. king
    Replies:
    5
    Views:
    182
Loading...

Share This Page