theredqueentheory has asked for the wisdom of the Perl Monks concerning the following question:
Please assist, I'm a biotech grad student trying to learn perl to make my experimental data easier to manipulate. I am writing a very simple program for conversions to start with, but am getting errors:
Any help is greatly appreciated!
my ($value, $from, $to, $prefix, %allprefixes);
%allprefixes =
(
"mega" => 6, #use these as exponents, so down below I can just
+ subtract exponents to get the answer
"kilo" => 3,
"milli" => -3,
"micro" => -6,
"nano" => -9,
"pico" => -12,
"femto" => -15,
"atto" => -18,
"zepto" => -21,
"yocto" => -24,
);
print "Enter the unit that you are starting with: ";
$from = <STDIN>;
print "Enter your target unit: ";
$to = <STDIN>;
print "Enter the numerical amount: ";
$value = <STDIN>;
chomp ($from);
chomp ($to);
chomp ($value);
if (not exists $allprefixes{$to}) {
die "My programmer has not yet supplied me with $to as a prefix\n"
+;
}
if (not exists $allprefixes{$from}) {
die "My programmer has not yet supplied me with $from as a prefix\
+n";
}
$prefix = $allprefixes{$to} -- $allprefixes{$from};
print "$value $from is ",$value*$prefix," $to. \n";
Re: syntax error at labmonkey.pl line 1, at EOF
by DigitalKitty (Parson) on Mar 04, 2008 at 18:43 UTC
|
Hi redqueentheory.
Welcome to PM.
Perhaps a minor point but I'd rewrite your code as:
use warnings;
use strict;
my $value = 0;
my $from = '';
my $to = '';
my $prefix = '';
my %prefixes = ();
%prefixes = (
mega => 6,
kilo => 3,
milli => -3,
micro => -6,
nano => -9,
pico => -12,
femto => -15,
atto => -18,
zepto => -21,
yocto => -24,
);
print "Enter the unit that you are starting with: ";
chomp( $from = <STDIN> );
print "Enter your target unit: ";
chomp( $to = <STDIN> );
print "Enter the numerical amount: ";
chomp( $value = <STDIN> );
if ( not exists $prefixes{$from} ) {
die qq/I don't know about the prefix $from\n/;
}
if ( not exists $prefixes{$to} ) {
die qq/I don't know about the prefix $to\n/;
}
$prefix = $prefixes{$from} - $prefixes{$to};
print "$value $from is ", $value * (10**$prefix), " $to. \n";
#Output:
C:\perl pm_test.pl
Enter the unit that you are starting with: mega
Enter your target unit: kilo
Enter the numerical amount: 10
10 mega is 10000 kilo.
Hope this helps,
~Katie
| [reply] [d/l] |
Re: syntax error at labmonkey.pl line 1, at EOF
by apl (Monsignor) on Mar 04, 2008 at 18:28 UTC
|
You need to fix $prefix = $allprefixes{$to} -- $allprefixes{$from}; A double negative sign is not valid in this statement.
I think you want a single minus sign for the exponent arithmetic. The final print needs some work as what it displays is misleading.
(You also need something like #!/usr/local/bin/perl as your first line.)
| [reply] [d/l] [select] |
|
Cool. I have downloaded the TextMate text editor and now it runs, must have been an AdobeGoLive problem.
I have searched for the OS units program, but don't seem to have it installed. Could you kindly provide a link?
I have searched online but have not found a place to download it.
| [reply] |
|
Eeww. Consider getting a real code editor (or learn a *NIX standard such as vim or emacs). XCode's builtin editor also would be a step up (presuming you have the OS X development tools installed).
And as a comment to your original program, you might consider using the OS' units program which already has a fairly good knowledge of conversions rather than reinventing the wheel piecemeal yourself.
$ units kilograms grams
* 1000
/ 0.001
$ units feet kilometers
* 0.0003048
/ 3280.8399
(Those two lines are what you multiply the later by to get the former and what you divide the former by to get the later)
Update: Duuur. Strike that, reverse it; as is noted below. Been so long since I've used it I forgot how it works. (At least I remembered it was there . . . :)
The cake is a lie.
The cake is a lie.
The cake is a lie.
| [reply] [d/l] |
|
|
| [reply] [d/l] |
Re: syntax error at labmonkey.pl line 1, at EOF
by Old_Gray_Bear (Bishop) on Mar 04, 2008 at 18:42 UTC
|
perl -c "labmonkey.pl"
Scalar found where operator expected at labmonkey.pl line 34, near "--
$allprefixes"
(Missing operator before $allprefixes?)
syntax error at labmonkey.pl line 34, near "-- $allprefixes"
labmonkey.pl had compilation errors.
When I ran your code.
I noticed that you didn't start the code with a 'shebang' line. May I suggest adding these lines to the top of your code:
#! /usr/local/bin/perl
use strict;
use warnings;
The shebang lets your OS get a clue about what language to expect; strict and warnings will give you a heads up about common mistakes and errors. ('variable used once, possible typo?' is one of my favorites -- I am a three fingered typist, all three fingers are on my left hand. I see that one a lot.)
Welcome to the Monastery, have fun with the Toys, don't forget to enjoy yourself.
----
I Go Back to Sleep, Now.
OGB
| [reply] [d/l] [select] |
Re: syntax error at labmonkey.pl line 1, at EOF
by ikegami (Patriarch) on Mar 04, 2008 at 18:47 UTC
|
That error doesn't sound like something Perl would give. Is that the error message exactly? Are you sure perl is being used?
And line 1 isn't near EOF. Are the line endings correct? Again, Perl shouldn't care about that.
| [reply] [d/l] |
Re: syntax error at labmonkey.pl line 1, at EOF
by graff (Chancellor) on Mar 05, 2008 at 03:32 UTC
|
I hope the previous replies have brought you to a point where you have a functioning and satisfying script. Now, I'd like to introduce you to my best friend... @ARGV, meet theredqueentheory. Red, meet @ARGV:
#!/usr/bin/perl
# (of course, "/usr/local/bin/perl" should work as well)
use strict;
if ( @ARGV != 3 ) {
die "Usage: $0 starting_unit target_unit numeric_amount\n";
}
my ( $from, $to, $value ) = @ARGV;
#use these as exponents; below we just subtract exponents to get the a
+nswer
my %prefixes = (
"mega" => 6,
"kilo" => 3,
"milli" => -3,
"micro" => -6,
"nano" => -9,
"pico" => -12,
"femto" => -15,
"atto" => -18,
"zepto" => -21,
"yocto" => -24,
);
for ( $from, $to ) {
next if ( exists( $prefixes{$_} ));
my $msg = "$_ is not a known unit. Try one of these:\n";
for my $prf (sort { $prefixes{$b}<=>$prefixes{$a} } keys %prefixes
+ ) {
$msg .= "\t$prf\n";
}
die $msg;
}
my $exp = $prefixes{$from} - $prefixes{$to};
print "$value $from is ", $value * 10 ** $exp, " $to.\n";
I think that produces the sort of result you were looking for (I adopted the mathematical corrections provided by DigitalKitty).
The virtues and bonuses of using @ARGV to get user input from args on the command line are numerous, and you will appreciate them more and more as time goes by.
Simple example: does your shell support "command history", where you are able to use the "up-arrow" key to get back to a previous command, edit it and run again? (All decent shells have this.) Consider wanting to run your script 5 times with different user inputs... what is easier: answering the same three questions repeatedly 5 times? or recalling a previous command, changing one or more args on the command line, and running it again? Try it both ways, and see which one you like better. | [reply] [d/l] |
Re: syntax error at labmonkey.pl line 1, at EOF
by Zen (Deacon) on Mar 04, 2008 at 23:31 UTC
|
Learning perl is well worth the time investment. Nothing beats it for easy reporting and analytics, IMHO. | [reply] |
|
|