[tpm] OID sorting

Indy Singh indy at indigostar.com
Sat May 2 08:58:15 PDT 2009


Without a real description of the problem, here is my attempt.  I used a simple scalar representation of data instead of a tree.  If someone would care to do a time comparison, I would be curious to see the results.


#!/usr/bin/perl

use strict;
use warnings;

my @list = (
    "1.3",
    "1.2.3",
    "1.2.3.1",
    "1.2.4.1",
    "1.2.30",
    "1",
    "1.2",
);
# The expected sort order is the same as the order above

sub byoid
{
  # convert dot seperated oid digits into a packed character string
  # suitable for string comparison, e.g. '65.66.67' => 'ABC'
  my $x;
  $x .= pack('C', $_) foreach (split(/\./, $a));
  my $y;  $y .= pack('C', $_) foreach (split(/\./, $b));
  return ($x cmp $y);
}

my @sorted = sort byoid @list;
print "Sorted list:\n", join("\n", @sorted), "\n";



sub byoid_slower_more_readable_version
{
  # convert dot seperated oid digits into digits with leading 0's
  # suitable for string comparison, e.g. '1.2.3' => '001002003'
  my $x;
  $x .= sprintf('%03d', $_) foreach (split(/\./, $a));
  my $y;
  $y .= sprintf('%03d', $_) foreach (split(/\./, $b));
  return ($x cmp $y);
}




Indy Singh
IndigoSTAR Software -- www.indigostar.com


  ----- Original Message ----- 
  From: Henry Baragar 
  To: Toronto Perl Mongers 
  Sent: Friday, May 01, 2009 2:46 PM
  Subject: [tpm] OID sorting


  Hello,




  There were a couple of suggestions for Fulko about how to improve his sorting of OID's (strings of the form ^\d+([.]\d+)*$). Attached is my suggestion for rebuilding the tree and walking the tree. I would be curious to see how this solution compares to the other solutions, both in terms of readability and performance.




  Regards,
  Henry





  -- 

  Henry Baragar
  Instantiated Software
  416-907-8454 x42



------------------------------------------------------------------------------


  _______________________________________________
  toronto-pm mailing list
  toronto-pm at pm.org
  http://mail.pm.org/mailman/listinfo/toronto-pm
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.pm.org/pipermail/toronto-pm/attachments/20090502/131d65c6/attachment.html>


More information about the toronto-pm mailing list