On 4/25/06, JupiterHost.Net <[EMAIL PROTECTED]> wrote:

>      sort {
>          $a->value() cmp $b->value()
>          ||
>          $a->part('Name')->value() cmp $b->part('Name')->value()
>      }
>      grep { defined } @objects

But sometimes $a->part('Name') returns undef, so the sort fails.

> I need all objects regardless of if their part call returns and object
> or undef :(
>
> Perhaps their is a logic I can do something like that, like 2 sort()s or
> a map() or ??

It almost sounds like you're talking about a Schwartzian Transform. In
fact, you could process your data in steps, as the Schwartzian
Transform does. (Warning: Untested code follows.)

First, you pick out the items you want:

  my @desired_data = grep { defined } @objects;

Next, transform each data item into an array reference holding the
original item and anything useful-to-know that you don't want to have
to recompute inside the sort block. In this case, that's your method
calls. I'm thinking something like this?

  my @transformed_data = map {
    my $part_name = $_->part('Name');
    my $part_name_value = (defined $part_name)
      ? $part_name->value()
      : "";    # empty string
    [ $_, $_->value(), $part_name_value ]
  } @desired_data;

Now you can sort efficiently on the derived data:

  my @sorted_data = sort {
    $a->[1] cmp $b->[1]    # value
    or
    $a->[2] cmp $b->[2]    # part_name_value
  } @transformed_data;

Finally, strip away the stuff you don't need anymore:

  my @result = map $_->[0], @sorted_data;

And you've got your data in order. Is that anything like the order you wanted?

In the tradition of the S.T., you should string the steps together
without the intermediate arrays. But if you've followed along up to
here, you can do that on your own.

Hope this helps!

--Tom Phoenix
Stonehenge Perl Training

--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>


Reply via email to