|
|
#1 |
|
Triple-A Player
Join Date: Jan 2002
Location: Chicago, Illinois
Posts: 231
|
I am wondering if I might be able to get the assistance of someone who really knows shell (or Perl) scripts fairly well.
I am on Weight Watchers at the moment. I've recently discovered the mathematical equation for calculating ‘points,' which is the way most people track their eating habits while on the program. I would love to be able to, at the Terminal, type in ‘wwpoints 130 5 1,' and have it just simply return the points, which in this case would be 2.8. The mathematical formula is: [calories + (4 * fat grams) - (10 * fiber grams)] / 50 = points So, for programming purposes, it would be: [first number on command line + (4 * second number on command line) - (10 * third number on command line)] / 50 = value returned by program So the output might look like: mharris% wwpoints 130 5 1 2.8 mharris% If you're looking for something to make it more complicated, if no values are entered, it could prompt for them: mharris% wwpoints Calories: 130 Fat Grams: 5 Fiber Grams: 1 2.8 mharris% This would actually be of use not only to me but to any Mac OS X user who happens to be on the Weight Watchers program. I would hope that you wouldn't mind me posting your script around to places like the Weight Watchers discussion board and newsgroup, and perhaps even as a Mac OS X Hints hint -- I would of course keep attribution intact. Or, I could just post a link to your post here. In any case, if anyone actually takes me up on this, my sincere and deepest thanks in advance. (I may also post this to comp.unix.questions, especially if no one is interested.) |
|
|
|
|
|
#2 |
|
Major Leaguer
Join Date: Jan 2002
Location: Adelaide, South Australia
Posts: 470
|
This should do what you're after: no need for attribution etc (do with it what you will!). You'll need to do the usual things with it: save it as "wwpoints" or whatever, throw it into ~/bin for example, "chmod u+x wwpoints", then "rehash". After that you should be set.
[Warning: written quickly, late at night. Let me know if there are errors/corrections that need to be made, or if you want it to act differently but can't achieve that.] Best wishes, Paul ------------------------------------------------ #!/usr/bin/perl -w use strict; use vars qw($calories $fat $fiber); if($#ARGV==2){ ($calories,$fat,$fiber)=@ARGV; }else{ prompt_for_values(); } my $points=($calories+(4*$fat)-(10*$fiber))/50; printf "This is worth %1.1f Weight Watcher points\n",$points; sub prompt_for_values{ no strict 'refs'; my @info=qw(calories fat fiber); foreach (@info){ print "Enter the number of $_",$_ eq 'calories'?'':' grams',": "; chomp(${$_}=<STDIN>); } } |
|
|
|
|
|
#3 |
|
Major Leaguer
Join Date: Jan 2003
Location: Portland, OR
Posts: 288
|
this is a quick script that I made that does the job. This doesn't do the prompting when no arguments are passed in.
<BEGIN> # @ calories = $1 @ fatGrams = $2 @ fiberGrams = $3 @ addition = $calories + (4 * $fatGrams) - (10 * $fiberGrams) @ points = $addition / 50 @ mod = $addition % 50 @ remainder = $mod / 5 echo "points=$points.$remainder"; <END> save the lines between "<BEGIN>" and "<END>" to a file (e.g. test.sh) and run it (e.g. /bin/tcsh test.sh) |
|
|
|
|
|
#4 |
|
Prospect
Join Date: Nov 2002
Location: Madrid, Spain
Posts: 33
|
Probably the easiest way of doing what you want is using the arbitrary precision calculator "bc". In your case (assuming that you use the default tcsh shell) type the following at the shell prompt:
Code:
alias wwp 'echo "scale=2; (\!:1 + (4 * \!:2) - (10 *\!:3))/ 50" | /usr/bin/bc' % wwp 130 5 1 2.80 To automate the process, just add the above code to either your .tcshrc or to ~/Library/init/tcsh/aliases.mine. |
|
|
|
![]() |
|
|