Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

(tye)Re3: The Definitive Unit Conversion Script

by tye (Cardinal)
on Dec 13, 2002 at 20:20 UTC ( #219739=note: print w/ replies, xml ) Need Help??


in reply to Re^3: The Definitive Unit Conversion Script
in thread The Definitive Unit Conversion Script

This doesn't have to be complicated. Split on * and before /, then check for /\^\d+$/:

my @units= split m#(?<=.)(?:(?=/)|\*)#, $units, -1; my %power; for my $unit ( @units ) { my $power= 1; if( $unit =~ s#\^(\d+)$## ) { $power= $1; } if( $unit =~ s#^/## ) { $power= -$power; } $power{$unit} += $power; } # ...
then you have to find units that you can convert to with matching powers. So, if you start with kg/cm/sec [ which becomes (kg=>1,cm=>-1,sec=>-1) ] and are asked to return lb/ft/hr (lb=>1,ft=>-1,hr=>-1), then you have to convert from kg to lb, then convert cm to either ft or hr (there will be no cm-to-hr path so you'll do cm-to-ft), then convert the left-overs, sec-to-hr. And you're done.

(Updated and tested.)

        - tye


Comment on (tye)Re3: The Definitive Unit Conversion Script
Download Code

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://219739]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others chilling in the Monastery: (11)
As of 2014-09-30 20:52 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    How do you remember the number of days in each month?











    Results (384 votes), past polls