Go Back   The macosxhints Forums > OS X Help Requests > UNIX - General



Reply
 
Thread Tools Rate Thread Display Modes
Old 04-25-2003, 10:35 AM   #1
WCityMike
Triple-A Player
 
Join Date: Jan 2002
Location: Chicago, Illinois
Posts: 231
Question Hoping to Automate Some Math Via tcsh or Perl

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.)
WCityMike is offline   Reply With Quote
Old 04-25-2003, 11:30 AM   #2
pmccann
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>);
}
}
pmccann is offline   Reply With Quote
Old 04-25-2003, 12:21 PM   #3
java_guy
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)
java_guy is offline   Reply With Quote
Old 04-25-2003, 02:53 PM   #4
artemio
Prospect
 
Join Date: Nov 2002
Location: Madrid, Spain
Posts: 33
Lightbulb

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'
From then on, you can use the wwp command as you suggested:

% 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.
artemio is offline   Reply With Quote
Reply


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump



All times are GMT -5. The time now is 06:08 PM.


Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2014, vBulletin Solutions, Inc.
Site design © IDG Consumer & SMB; individuals retain copyright of their postings
but consent to the possible use of their material in other areas of IDG Consumer & SMB.