Tux

...making Linux just a little more fun!

Current Issue calculation

Joey Prestia [joey at linuxamd.com]


Mon, 06 Sep 2010 20:34:43 -0700

Hi Tag,

I am trying to rework a script that currently uses an external file to keep track of what issue the Linux Gazette is on. I would like to do this with out relying on an external file (feels cleaner that way) and just calculate this from within the script using maybe the month and year from localtime(time) from within Perl. Using the month and year I thought this would be an easy task but it turns out its more difficult than I thought. I will probably need some formula to do It to since I will be running it from cron. Can you make any suggestions on how I might attempt this? I have tried to figure a constant that I could use to get it to come out correct with no luck. What works for one year fails when the year changes when you add the month to the year.

# Get Issue my (@date,$month,$year,$issue);

@date = localtime(time); $month=($date[4])+1; $year=($date[5])+1900;

$issue= $year - 1841 + $month ; print "Month = $month Year = $year Issue = $issue\n";

Joey


Top    Back


Ben Okopnik [ben at linuxgazette.net]


Tue, 7 Sep 2010 00:29:58 -0400

Hi, Joey -

On Mon, Sep 06, 2010 at 08:34:43PM -0700, Joey Prestia wrote:

>  Hi Tag,
> 
> I am trying to rework a script that currently uses an external file to
> keep track of what issue the Linux Gazette is on. I would like to do
> this with out relying on an external file (feels cleaner that way) and
> just calculate this from within the script using  maybe the month and
> year from localtime(time) from within Perl.

That won't work - there's no linear correspondence like that. As I recall, we've had months with two issues - and we've had months without an issue (before I took over); just take a look at http://linuxgazette.net/archives.html .

> Using the month and year I
> thought this would be an easy task but it turns out its more difficult
> than I thought.  I will probably need some  formula to do It to  since I
> will be running it from cron. Can you make any suggestions on how I 
> might attempt this?

You could always cheat and use the archives - live. :)

#!/usr/bin/perl -w
# Created by Ben Okopnik on Mon Sep  6 23:45:27 EDT 2010
use strict;
 
die "Usage: ", $0 =~ /([^\/]+)$/, "<file|-> <m> <YYYY>\n"
	unless @ARGV == 3 and $ARGV[1] =~ /^\d{1,2}$/ and $ARGV[2] =~ /^\d{4}$/;
 
my($m, $y) = splice @ARGV, 1;
 
my $re = qr/^\s(.{4})\s/ . qr/(.{9})/ x 12;
my %issue;
while (<>){
	tr/0-9\t\n //dc;
	next unless /\d{4}/;
	my @line = /$re/;
	$issue{$line[0]} = [ @line[1..$#line] ];
}
 
print $issue{$y}->[$m - 1], "\n";
ben at Jotunheim:~$ w3m -dump linuxgazette.net/archives.html|archie - 9 2010
 178
ben at Jotunheim:~$ w3m -dump linuxgazette.net/archives.html|archie - 4 1999
 39 40
ben at Jotunheim:~$ w3m -dump linuxgazette.net/archives.html|archie - 2 1996
 
ben at Jotunheim:~$

(There was no issue on 2/96, so, no output.)

-- 
* Ben Okopnik * Editor-in-Chief, Linux Gazette * http://LinuxGazette.NET *


Top    Back


Ben Okopnik [ben at linuxgazette.net]


Tue, 7 Sep 2010 00:56:01 -0400

On Mon, Sep 06, 2010 at 08:34:43PM -0700, Joey Prestia wrote:

> 
> I am trying to rework a script that currently uses an external file to
> keep track of what issue the Linux Gazette is on.

Just thought of this: the output could be a bit more obvious/readable.

#!/usr/bin/perl -w
# Created by Ben Okopnik on Mon Sep  6 23:45:27 EDT 2010
use strict;
 
die "Usage: ", $0 =~ /([^\/]+)$/, "<file|-> <m|mm> <YYYY>\n"
	unless @ARGV == 3 and $ARGV[1] =~ /^\d{1,2}$/ and $ARGV[2] =~ /^\d{4}$/;
 
my($m, $y, %issue) = splice @ARGV, 1;
 
my $re = qr/^\s(.{4})\s/ . qr/\s{1,4}(.+?)\s{1,4}/ x 12;
while (<>){
	next unless /\d{4}/;
	tr/0-9\t\n //dc;
	my @line = /$re/;
	$issue{$line[0]} = [ @line[1..$#line] ];
}
 
print "$m/$y: [$issue{$y}->[$m-1]]\n";

Output now looks like:

2/1996: [ ]
4/1999: [39 40]
9/2010: [178]
-- 
* Ben Okopnik * Editor-in-Chief, Linux Gazette * http://LinuxGazette.NET *


Top    Back


Joey Prestia [joey at linuxamd.com]


Wed, 08 Sep 2010 07:05:56 -0700

On 9/6/2010 9:29 PM, Ben Okopnik wrote:

> 
> You could always cheat and use the archives - live. :)
> 

That looks like an excellent choice. Its already in existence.

> 
> ``
> ben at Jotunheim:~$ w3m -dump linuxgazette.net/archives.html|archie - 9 2010
>  178

Never thought to use w3m. :) Cool thanks.

-- 
Joey Prestia
L. G. Mirror Coordinator
http://linuxgazette.net


Top    Back


Ben Okopnik [ben at linuxgazette.net]


Wed, 8 Sep 2010 12:32:31 -0400

On Wed, Sep 08, 2010 at 07:05:56AM -0700, Joey Prestia wrote:

> On 9/6/2010 9:29 PM, Ben Okopnik wrote:
> 
> > 
> > You could always cheat and use the archives - live. :)
> > 
> 
> That looks like an excellent choice. Its already in existence.
Yeah, I try to be as efficiently lazy as possible.

> > ``
> > ben at Jotunheim:~$ w3m -dump linuxgazette.net/archives.html|archie - 9 2010
> >  178
> 
> Never thought to use w3m. :) Cool thanks.

By this point, I know the LG data structure so well that parsing it is just about automatic. And, as in the case of this one, kinda fun. :)

-- 
* Ben Okopnik * Editor-in-Chief, Linux Gazette * http://LinuxGazette.NET *


Top    Back