Loading ...
Sorry, an error occurred while loading the content.

Re: question on cmp operator.....

Expand Messages
  • chcst53
    ... If the last name comes after the first name, separated by a comma, then your split above puts the last name into $first_one and $first_two . In your
    Message 1 of 2 , Jul 3, 2004
    • 0 Attachment
      --- In perl-beginner@yahoogroups.com, Kathryn Tate
      <kathryntate@c...> wrote:
      > Here is my algorithm:
      > @employee = sort
      > {
      > ($last_one, $first_one)=split(",", $a);
      > ($last_two, $first_two)=split(",", $b);
      > return($first_one cmp $first_two ||
      > $last_one cmp $last_two);
      > } @employee;
      >
      > I am trying to sort on the employee last name which comes
      > after first name in this case. This compiles but does not sort.
      >
      >
      > Thanks in advance,
      > Kathryn

      If the last name comes after the first name, separated by a comma,
      then your split above puts the last name into '$first_one'
      and '$first_two'.

      In your sort with cmp, you are saying above to sort on the first
      name first and then second, on the last name.

      I'm really not clear on these 2 points (above).
      If, following usual convention, you were to sort on the last name
      and then on the first name, Ive made some adjustments AND assume the
      last name comes first followed by a comma and then the first name.

      :#!/usr/bin/perl
      :use strict;
      :use warnings;
      :
      :chomp(my @employee = <DATA>);
      :@employee = sort {
      : my ($last_one, $first_one)=split(",", $a);
      : my ($last_two, $first_two)=split(",", $b);
      : return($last_one cmp $last_two || $first_one cmp $first_two);
      :} @employee;
      :
      :print join "\n", @employee;
      :
      :__DATA__
      :Smith,Tom
      :Zee,Larry
      :Smith,Larry
    Your message has been successfully submitted and would be delivered to recipients shortly.