APLawrence.com -  Resources for Unix and Linux Systems, Bloggers and the self-employed

A Perl script for tracking nutrition


2012/08/06

I have said before that I feel sorry for people who can't do simple scripting. If that's you, you are limiting yourself greatly and undoubtedly missing opportunities to use your computer more efficiently.

For example, I recently got a very bad cholesterol report from my doctor. I think the lab screwed up - they have done so before and a re-test has shown more reasonable levels. However, my doctor is insistent that the results are real and that I must change my eating habits.

Unfortunately, there's not much I can do in that regard. I already follow a healthy diet and can't take advantage of his suggestions to "avoid soft drinks and fatty beef" as I already do that. I already have a healthy life style.

However, I didn't have hard data to back up my claim. I therefore decided to track my food intake and exercise over the next three months; I'll then be able to show him exactly what I eat and what my level of physical activity is. We can then argue more intelligently about the proper response to one nutty blood test.

Tracking Nutrition

There are on-line programs that can help you track your diet and exercise, but I found them to be clumsy and of course rife with advertisements as well as containing data I don't care about (I'm uninterested in my protein and carbohydrate intake, for example). I just want to track calories, total fat and saturated fat (I never eat anything with trans fat, so I don't need to track that, either).

I started my tracking with simple text files. For example, here is my typical morning food intake:

6:00 coffee yogurt: 160 2.5 1.5
Grape juice 6 oz: 126 0 0
Sm handful peanuts: 55 4 0.6
black coffee: 5 0 0
72% Chocolate 2 sq:  56  4.52  2.6 
9:30 Oatmeal with banana and blueberries: 255 .3 .1
Grape juice 8 oz: 170 0 0 
 

The numbers are calories, total fat and saturated fat. I wrote a simple Perl script which will allow me to paste in all the text and get each column totaled:

$ addit3
6:00 coffee yogurt: 160 2.5 1.5
Grape juice 6 oz: 126 0 0
Sm handful peanuts: 55 4 0.6
black coffee: 5 0 0
72% Chocolate 2 sq:  56  4.52  2.6 
9:30 Oatmeal with banana and blueberries: 255 .3 .1
Grape juice 8 oz: 170 0 0 
827 11.32 4.8
$
 

Because I also want to track exercise, I wrote another script that will take lines like these:

chores: 30
8:00 pushups: 35
8:30 pullups: 10
9:00 pullups: 10
9:10 stepups:20
9:20 lunges:20
9:55 pushups: 27
11:00 Gardening: 45
1:20 balancing: 4
1:45 crunches:10
4:50 stretching : 5
6:30 stretching:8
7:00 stretching:8
8:00 stretching :2
Walking: 35677
AEROBIC  TIME: 77
AEROBIC DISTANCE: 20628
 

That script produces a daily report that looks like this:

AEROBIC  TIME 77
AEROBIC DISTANCE 20628
BALANCING 4
CHORES 30
CRUNCHES 10
GARDENING 45
LUNGES 20
PULLUPS 20
PUSHUPS 62
STEPUPS 20
STRETCHING 23
WALKING 35677
 

Pedometer

I get the walking and aerobic figures from an Omron HJ-112 Pedometer. These modern electronic pedometers are wonderful - I just carry it in my shirt pocket. It doesn't matter if it is upside right or upside down; it accurately records my walking activity. On the day above, I walked a total of 35,677 feet, of which 77 minutes and 20,628 feet were aerobic (more than ten minutes without stopping). That's a lot easier than trying to keep track of this myself!

The scripts

These can be easily extended to track more columns, of course.

#!/usr/bin/perl
$x=0;
while (<>) {
  s/,//g;
  print "ERROR $_" if not /:/;
  @s=split /:/;
  shift @s if $s[2];
  $s[1]=~ s/^\s*//;
  ($c,$f,$s)=split /\s+/, $s[1];
  $tc=$tc+$c;
  $tf=$tf+$f;
  $ts=$ts+$s;
}
print "$tc $tf $ts\n";
 
#!/usr/bin/perl
while (<>) {
  chomp;
  next if /^$/;
  s/,//g;
  s/^\s+//;
  s/^[0-9]:[0-9][0-9]//;
  s/^[0-9][0-9]:[0-9][0-9]//;
  s/^\s+//;
  s/\s+:/:/;
  print "Error $_\n"  if not /:/;
  ($t,$a)=split /:/;
  $t=uc($t);
  $types{$t}+= $a;
  
}
print "====\n";
foreach (sort keys %types) {
  print "$_ $types{$_}\n";
}

Why not a spreadsheet?

Because this is quicker and easier and I can feed the output to other scripts that accumulate monthly figures, do transformations and so on. A spreadsheet is a very limited tool.

With another formatting script, I end up with a final report that looks like this:

Calories = 54 days avg 1995.98 Min 1376 max 2607 std dev.  278.78
Fat = 54 days avg 58.01 Min 33.9 max 112.38 std dev.  14.70
Saturated Fat = 54 days avg 13.85 Min 6.9 max 29.8 std dev.  4.03
 
==== 54 days
AEROBIC WALKING 57 sets averages  1.41 (per set)  1.49 (per day) highest 3.91
AEROBIC TIME 57 sets averages  29.84 (per set)  31.50 (per day) highest 96.00
AEROBIC HEART RATE 26 sets averages  121.04 (per set)  highest 156.00
MISC. WALKING 54 sets averages  2.25 (per set)  2.25 (per day) highest 6.50
VIGOROUS 16 sets averages  4.50 (per set)  1.33 (per day) highest 15.00
VIGOROUS HEART RATE 12 sets averages  136.17 (per set)  highest 156.00
LUNGES 27 sets averages  21.85 (per set)  10.93 (per day) highest 35.00
PULLUPS 61 sets averages  9.00 (per set)  10.17 (per day) highest 12.00
PUSHUPS 37 sets averages  28.43 (per set)  19.48 (per day) highest 40.00
DIPS 19 sets averages  18.84 (per set)  6.63 (per day) highest 20.00
STEPUPS 29 sets averages  26.90 (per set)  14.44 (per day) highest 60.00
CRUNCHES 26 sets averages  15.08 (per set)  7.26 (per day) highest 30.00
STEPUPS 29 sets averages  26.90 (per set)  14.44 (per day) highest 60.00
BALANCING 18 sets averages  3.56 (per set)  1.19 (per day) highest 5.00
STRETCHING 60 sets averages  3.78 (per set)  4.20 (per day) highest 20.00



Got something to add? Send me email.





(OLDER)    <- More Stuff -> (NEWER)    (NEWEST)   

Printer Friendly Version

-> -> A Perl script for tracking nutrition


4 comments



Increase ad revenue 50-250% with Ezoic


More Articles by

Find me on Google+

© Anthony Lawrence







Tue Aug 7 16:25:06 2012: 11230   BigDumbDinosaur

gravatar


Why not a spreadsheet?

Succinct answer: lack of flexibility.

Why is it anyone would think a spreadsheet is a database or an all purpose tool to keep data organized? How did we ever manage before Visi-Calc came on the scene? (Rhetorical question.) For that matter, how did we ever manage to survive without Windows?





Mon Sep 10 19:29:26 2012: 11292   Anonymous

gravatar


Saturated fat has pretty much been exonerated as the cause of heart disease, obesity, etc., and may even be good for you! Diets high in simple carbohydrates and polyunsaturated oils--you know, "healthy" stuff like fruit juice, grains, and margarine--are what cause the chronic health problems.

Looks like your doctor's knowledge of nutrition is from the same time period as VisiCalc. :-P



Mon Sep 10 19:55:16 2012: 11293   TonyLawrence

gravatar


If you want to use that as an excuse to kill yourself, be my guest.







Tue Sep 11 16:40:36 2012: 11298   BigDumbDinosaur

gravatar


Saturated fat has pretty much been exonerated as the cause of heart disease, obesity, etc., and may even be good for you!

Really! I don't recall reading anything like that. My hematologist/oncologist, who is a professor at the University of Chicago School of Medicine and also runs the cancer treatment center at Silver Cross Hospital in New Lenox (IL), has long exhorted me to minimize foods that are a source of saturated fat, as well as get a reasonable amount of exercise. I'd take his word any day over that of an anonymous post on a website. As Tony said, you have the right to kill yourself with clogged arteries, etc.

Looks like your doctor's knowledge of nutrition is from the same time period as VisiCalc.

The adverse effects of a high fat diet were well-known long before Visi-Calc came on the scene. In fact, it has often been noted that Asian people, on average, have a much lower risk of cardiovascular problems than we Westerners. The generally accepted reason for this is that their diets are relatively low in fat and include plenty of fruit and grains, things that you assert are bad for you. What's really interesting is that Asian people living in the USA who don't following the Asian diet (which has more fish and fowl than beef) tend to have cardiovascular issues like we fat Westerners. So your assertion that healthy isn't really healthy is suspect -- and I'm being kind.

------------------------
Kerio Samepage


Have you tried Searching this site?

Unix/Linux/Mac OS X support by phone, email or on-site: Support Rates

This is a Unix/Linux resource website. It contains technical articles about Unix, Linux and general computing related subjects, opinion, news, help files, how-to's, tutorials and more.

Contact us





Doing linear scans over an associative array is like trying to club someone to death with a loaded Uzi. (Larry Wall)

The only thing I'd rather own than Windows is English. Then I'd be able to charge you an upgrade fee every time I add new letters like N and T. (Scott McNealy)












This post tagged: