Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
#!/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":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others scrutinizing the Monastery: (6)
As of 2024-04-19 12:55 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found