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

RE: [PBML] Special-case sort function

Expand Messages
  • Charles K. Clarkson
    Jean-Sébastien Guay wrote: There s probably an more efficient algorithm, but: #!/usr/bin/perl use strict; use warnings; my
    Message 1 of 2 , Mar 28 8:02 AM
    • 0 Attachment
      Jean-Sébastien Guay <jean_seb@...> wrote:

      There's probably an more efficient algorithm, but:

      #!/usr/bin/perl

      use strict;
      use warnings;

      my @unordered_tasks = (
      'Animation',
      'Modeling',
      'Graphics',
      'Compositing',
      );

      my @task_order = (
      'Sketch',
      'Design',
      'Modeling',
      'Color Scheme',
      'Texture',
      'Rigging',
      'Tracking',
      'Layout',
      'Animation',
      'Anaglyph',
      'Particle',
      'Lighting',
      'Compositing',
      );

      my @sorted_tasks = sort_tasks( \@task_order, \@unordered_tasks );

      sub sort_tasks {
      # get two array references
      my( $task_order, $unordered_tasks ) = @_;

      # create a hash to hold the order
      my %task_order;

      # use a hash slice to fill the value order
      # @$task_order return the size of the array
      @task_order{ @$task_order } = 1 .. @$task_order;

      # adjust the hash to add extra keys to the end of the order
      # @$task_order + 1 = the size of the array plus 1
      # ||= assigns a high number only if the key is not defined
      $task_order{ $_ } ||= @$task_order + 1 foreach @$unordered_tasks;

      #return an array of sorted tasks based on task_oder hash
      return
      sort
      { $task_order{ $a } <=> $task_order{ $b } }
      @$unordered_tasks;
      }

      __END__

      HTH,

      Charles K. Clarkson
      --
      Head Bottle Washer,
      Clarkson Energy Homes, Inc.
      Mobile Home Specialists
      254 968-8328
    Your message has been successfully submitted and would be delivered to recipients shortly.