ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
foreach ($weights as $w) { if (array_key_exists($key, $array)) { $sum += $w * $array[$key]; $div += $w; } $key++; }
if ($div == 0) return FALSE;
return $sum / $div; }
where the data is in the first parameter, the second parameter is the key for the first month of the window, and the third parameter contains the desired weights for the desired window, by default just one month. For a sliding quarter year, you could use array(1,1,1). For a half year, array(1,1,1,1,1,1). For a Gaussian distribution centered on the fourth month with one month variance use array(0.006,0.061,0.242,0.383,0.242,0.061,0.006) .
The averaging function above will correctly ignore months with missing data. The weights are relative, they do not need to sum up to one. If there is no data at all, the function will return FALSE.
You should be able to tweak the above to suit your purposes. For example, it might be easier to define separate functions for the sliding average you'll be using, and define the weight array in the function (or as a global variable) instead.
for ($arg = 1; $arg < $argc; $arg++) { $result = Quarter($data, $argv[$arg]); if ($result === FALSE) echo $argv[$arg], ": No data for such quarter.\n"; else echo $argv[$arg], ": Quarter average is ", $result, ".\n"; }
?>
The YearMonth function takes any year and month pair (or YYYYMM as a single number or string), and returns it as a suitable key. It's rather clever, using string manipulation tricks, so that you can use e.g. 2009jan or "Feb-2010" or "August, 2011" or even '02/2010' or '2011/8' and all will just work.
If you save the above as quarter.php , you can run it (after marking it executable, chmod u+x quarter.php) using e.g.
Code:
./quarter.php '2009 Jan' Feb-2010 "August, 2011"
The answers it reports are 250 (=(260+220+270)/3), 250 (=(210+260+280)/3), and 241.333 (=(250+230+244)/3).
Since you are so keen on just using a sliding quarter-year window, I simplified the averaging function to do that and that alone; it is named Quarter here. I recommend you compare it with the weighted_average function. Mathematically the Quarter logic is trivial: the average is the sum of items divided by the number of items. A quarter is three months (12 months / 4 quarters = 3 months / quarter). Again, if you don't have data for some specific month, the function will skip it; if there is no data for the period at all, it'll return FALSE.
Note that you do not need to transcribe the data to an array by hand. If you read the lines using fgets(), then compact all whitespace (using for example $line = trim(preg_replace('/[\t\n\v\f\r ]+/', ' ', $line)); ), you can explode the line into an array using $fields = explode(' ', $line); The length of the array (count($fields)) will be one more than the number of years you have. The first field, $field[0], will contain 'Month' on the header line, and month name on the other fields. On the header line, the rest of the fields state the years; they contain the values for other lines. Since you can populate an associative array in any order you want, it should be very easy to read the data array from a file using the table format from your first post in this thread.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.