
oldrob
Novice
Mar 11, 2013, 4:16 PM
Post #1 of 1
(723 views)
|
using utf characters in variables
|
Can't Post
|
|
Hello friendly Perl Community, I've been trying to produce a molarity calculator as a project (Im a chemist, it seemed sensible at the time). The code allows input of a scalar + the appropriate units. This input is split into the scalar number and the units string, the latter which is used to call a coefficient from a hash. The scalar and units can then be used in the molarity calculation. I was hoping to use the µ symbol when describing units - however this appears to break perl. I do have utf8 enabled - is there anyway around this? I post my code below. Also constructive criticism of the code is much appreciated!
#!/usr/bin/perl use warnings; use utf8; use bignum; #using a hash to set up coefficients for each dimension my %units = ( nmoles => "1e-9", nL => "1e-9", nM => "1e-9", micromoles => "1e-6", #cant use µmoles as this breaks perl microL => "1e-6", microM => "1e-6", mmoles => "1e-3", mL => "1e-3", mM => "1e-3", moles => "1e0", L => "1e0", M => "1e0" ); print "what would you like to work out? Moles (1), Volume (2) or Molarity (3)?\n"; $input = <STDIN>; if ($input == 1) { print "working out the number of Moles...\n"; print "what is the Molarity?\n"; $molarty = <STDIN>; ($molarity, $units1) = split(/ /,$molarty); print "What is the volume?\n"; $volum = <STDIN>; ($volume, $units2) = split(/ /,$volum); chomp($units1); chomp($units2); print "The volume is $volume $units2 and the molarity is $molarity $units1\n"; my $units1 = $units{$units1}; my $units2 = $units{$units2}; my $moles = $molarity*$units1*$volume*$units2; print "There are $moles moles"; } if ($input == 2) { print "Working out the volume...\n"; print "What is the molarity?\n"; $molarty = <STDIN>; ($molarity, $units1) = split(/ /,$molarty); print "How many moles are there?\n"; $mol = <STDIN>; ($moles, $units2) = split(/ /, $mol); #splits the input into the number and the units (string) chomp($units1); chomp($units2); print "The molarity is $molarity $units1 and there are $moles $units2 \n"; my $units1 = $units{"$units1"}; my $units2 = $units{"$units2"}; my $volume = (($moles*$units2)/($molarity*$units1)); print "The volume of the solution is $volume Litres"; } if ($input == 3){ print "working out the molarity...\n"; print "what is the volume?\n"; $volum = <STDIN>; ($volume, $units1) = split(/ /,$volum); #splits the input into the number and the units (string) print "what is the number of moles?\n"; $mol = <STDIN>; ($moles, $units2) = split(/ /, $mol); #splits the input into the number and the units (string) chomp($units1); chomp($units2); print "The volume is $volume $units1 and the there are $moles $units2\n"; my $units1 = $units{"$units1"}; my $units2 = $units{"$units2"}; print "$moles $units2 $volume $units1 \n"; my $molarity = ((($moles)*($units2))/(($volume)*($units1))); print "The molarity of the solution is $molarity M"; }
|