#!/usr/bin/perl use strict; use warnings; use Data::Dumper; use Math::BigInt lib => 'GMP'; my @speeds = ( '10Mbps', '115kbps', 'Flash', '2Mbps', 'T1 LINE', 'Shocker' ); my ( @clean_speeds, @sorted_speeds ); my $speed_re = { 'BPS' => qr/(\d+)\s*([KMGT])BPS/, # 10MBPS 'T' => qr/T(\d)\s*\w*/, # T1 LINE }; my $bps = Math::BigInt->new('2')->bpow(10); my $unit = { 'BPS' => { K => $bps->copy->bpow(0), M => $bps->copy->bpow(1), G => $bps->copy->bpow(2), T => $bps->copy->bpow(3), }, # ref => http://ckp.made-it.com/t1234.html 'T' => { 1 => Math::BigInt->new('1544'), # T1 = 1544 kbps 2 => Math::BigInt->new('6312'), # T2 = 6312 kbps 3 => Math::BigInt->new('44736'), # T3 = 44736 kbps 4 => Math::BigInt->new('274760'), # T4 = 274760 kbps }, }; for (@speeds) { my $speed = uc $_; $speed =~ /(?:$speed_re->{'BPS'}|$speed_re->{'T'})/ ? push @clean_speeds, $speed : warn "Unmatched speed: $speed\n"; } @sorted_speeds = sort { net_speed($a) <=> net_speed($b) } @clean_speeds; print Dumper \@sorted_speeds; sub net_speed { my $speed = shift; if ( $speed =~ /$speed_re->{'BPS'}/ ) { Math::BigInt->new( $unit->{'BPS'}->{$2} )->bmul($1); } elsif ( $speed =~ /$speed_re->{'T'}/ ) { Math::BigInt->new( $unit->{'T'}->{$1} ); } } __END__ Output: ------ Unmatched speed: FLASH Unmatched speed: SHOCKER $VAR1 = [ '115KBPS', 'T1 LINE', '2MBPS', '10MBPS' ];

In reply to Re: How do I write my own sorting routine? by poolpi
in thread How do I write my own sorting routine? by Russ

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.