ONJava.com -- The Independent Source for Enterprise Java
oreilly.comSafari Books Online.Conferences.

advertisement

AddThis Social Bookmark Button
Hack:   Command Line Calculator
Subject:   Your Missing the point && optimized version
Date:   2005-02-22 09:24:20
From:   AdamJacobMuller
While bc and others are great unix command line calculators, there is one thing that they can't do. Conversions. While bc is a fantastic application how long would it take you, using the command line, to tell me how man lbs are in 10 tons in bc?
unless you know by heart that there are 2000 lbs per ton, google calculate is going to be faster.
On that note, here is my optimized version of this script

#!/usr/bin/php -q
<?
preg_match_all("{<b>.+= (.+?)}",file_get_contents("http://www.google.com/search?q=".urlencode(join("+", array_splice($argv, 1)))), $matches);
print strip_tags($matches[1][0])."\n";
?>

I removed some things that had no place IMHO,
The entire last line, what was the purpose of that? strip_tags is better and faster than str_replace.
Ideally we would wrap this in a

if (count($argv)>1) {
preg_match_all("{.+= (.+?)}",file_get_contents("http://www.google.com/search?q=".urlencode(join("+", array_splice($argv, 1)))), $matches);
print strip_tags($matches[1][0])."\n";
} else {
print $argv[0].": <some shit>";
}

Like i said, it's all IMHO so.....

1 to 1 of 1
  1. Your Missing the point && optimized version
    2007-02-06 16:31:31  donovandak [View]

1 to 1 of 1