Well, for that, how do you define a leap year?
Leap year is every year the number of which is a multiple of four, except those devisible by 100 and not by 400.
Here are the steps to determine if a given year is a leap year or not:
if year modulo 400 is 0 then leap
else if year modulo 100 is 0 then no_leap
else if year modulo 4 is 0 then leap
else no_leap
Here is one Perl implementation.
#!/usr/bin/perl
use strict;
use warnings;
###### is_leap_year ######
# Purpose : To determine whether a given year is a leap year or not
# Arguements : One
# First (Number): Year to be checked
# Returns : Boolean value indicating whether given year is a leap year or not
# true = given year is a leap year
# false = given year is not a leap year
############################
sub is_leap_year ($)
{
my $year = shift;
return unless ($year =~ m/^\d+$/);
if (($year % 400) == 0) {
# This year is completely divisible by 400
return 1; # This is a leap year
}
elsif (($year % 100) == 0) {
# This year is completely divisible by 100
return 0; # This is not a leap year
}
elsif (($year % 4) == 0) {
# This year is completely divisible by 4
return 1; # This is a leap year
}
return 0; # This is not a leap year
}
my $this_year = 2040;
if (is_leap_year ($this_year)) {
print $this_year, " is a leap year\n";
}
else {
print $this_year, " is a not a leap year\n";
}
Thursday, July 31, 2008
Wednesday, July 30, 2008
Count number of occurrences of a word
How can you count number of occurrences of a word in a file? Say, I want to count how many times ring is present in a file.
How about using the -c option of grep?
% grep -c ring /foo/bar
And what if the file contains words such as string and rings? That would break our count of ring.
Using the -w option of grep solves this problem
% grep -cw ring /foo/bar
What if the word ring is present more than once in a line? grep would produce incorrect count in this case, since it counts the number of lines in which the pattern is found. grep is not sufficient for this job. We need something that can count multiple occurrences of a word in a line.
To get the exact count:
#!/usr/bin/perl
# search_word.pl
my $search_this = shift or exit 1;
my $count = 0;
while (<>) {
while (m/\b$search_this\b/g) {
$count++;
}
}
if ($count == 0) {
print $ARGV . "does not contain " . $search_this . "\n";
}
else {
print $ARGV . "contains " . $search_this . " " . $count . (($count == 1) ? " time\n" : " times\n");
}
Run this perl script as:
% perl search_word.pl ring filename
And what if I don't want to use Perl? Well, then this should work for you:
% grep -w -o ring /foo/bar | wc -l
How about using the -c option of grep?
% grep -c ring /foo/bar
And what if the file contains words such as string and rings? That would break our count of ring.
Using the -w option of grep solves this problem
% grep -cw ring /foo/bar
What if the word ring is present more than once in a line? grep would produce incorrect count in this case, since it counts the number of lines in which the pattern is found. grep is not sufficient for this job. We need something that can count multiple occurrences of a word in a line.
To get the exact count:
#!/usr/bin/perl
# search_word.pl
my $search_this = shift or exit 1;
my $count = 0;
while (<>) {
while (m/\b$search_this\b/g) {
$count++;
}
}
if ($count == 0) {
print $ARGV . "does not contain " . $search_this . "\n";
}
else {
print $ARGV . "contains " . $search_this . " " . $count . (($count == 1) ? " time\n" : " times\n");
}
Run this perl script as:
% perl search_word.pl ring filename
And what if I don't want to use Perl? Well, then this should work for you:
% grep -w -o ring /foo/bar | wc -l
Tuesday, July 29, 2008
Why not to use awk when sed can do the job?
- Using awk instead of sed has the price of performance and size
- compared to sed and ed, awk takes a substantially longer time to load, and does its job at a considerably slower pace
- The real distinguishing point between sed and awk as a text processor is that awk is able to work with a persistent context, whereas capabilities of sed in this area are limited to non-existent. If you - for instance - would have to sum one field to a total you would do it with awk (it would be possible to do it with sed, but would be a nightmare - poorly suited tool for the job)
Monday, July 28, 2008
Changing size of a file
How do you change the size of a file in Linux? If you are not concerned about the contents of the file, use the dd command.
So if you want to change the size to 2 MB
Note that 2 MB is 2048 KB, and not 2000 KB.
dd if=/dev/zero of=/foo/bar bs=1024 count=1
This would change the size of a file to 1 KB. inode number of the file would be preserved.So if you want to change the size to 2 MB
dd if=/dev/zero of=/foo/bar bs=1024 count=2048
Note that 2 MB is 2048 KB, and not 2000 KB.
Labels:
Linux
Sunday, July 27, 2008
Epigram on a Software Laird
Bless Linus Torvalds, O Engineer,
With grateful, lifted eyes,
Who taught that not the windows alone,
But doors too shall open;
For had He said "the doors alone
From source I will deliver,"
Alas, alas! O Engineer,
Then hadst thou lain for ever.
Original work: Epigram on a Country Laird by Robert Burns
With grateful, lifted eyes,
Who taught that not the windows alone,
But doors too shall open;
For had He said "the doors alone
From source I will deliver,"
Alas, alas! O Engineer,
Then hadst thou lain for ever.
Original work: Epigram on a Country Laird by Robert Burns
Labels:
Epigram
Tuesday, July 22, 2008
Sorting alphanumeric strings in Perl
One of my friends had trouble sorting alphanumeric strings in Perl. Strings he had were similar to:
He wanted to sort them based on the numbers that follow after the alphabtic part. So, the order he wanted:
I extracted the numeric part from the alphanumeric string and used it for comparisons
child1
child3
child11
child51
child5
child24
child3
child11
child51
child5
child24
He wanted to sort them based on the numbers that follow after the alphabtic part. So, the order he wanted:
child1
child3
child5
child11
child24
child51
child3
child5
child11
child24
child51
I extracted the numeric part from the alphanumeric string and used it for comparisons
@alphanumeric_sorted = sort { substr($a, 5) <=> substr($b, 5)} @alphanumeric;
Monday, July 21, 2008
Apache Lyrics
Sometime last year I wrote parody of a famous song. Some of the lines of this song are in Spanish, and you need to know a bit of Spanish to understand them.
"Ports Won't Die"
(feat. Office Assistant)
Macros up in here tonight
No fighting, no fighting
We got the fixes up in here
No fighting, no fighting
Apache, Apache
I never really knew that it could serve so fast
It makes a man wants to HTTP
Como se llama (si), bonita (si), mi OS (si, Apache Apache), su OS
Apache, Apache
Oh baby when you talk like that
You make IIS go mad
So be wise and keep on
Reading the signs of my Ballmer
And I'm on tonight
You know my ports won't die
And I'm starting to feel it's right
All the attraction, the tension
Don't you see baby, this is perfection
Hey Girl, I can see your traffic moving
And it's driving me crazy
And I didn't have the slightest idea
Until I saw you serving
And when you take up a bad request
Nobody cannot ignore the way you check it out, girl
And architecture so perfected - the way you right and left it
So you can keep on excelling
I never really knew that it could serve so fast
It makes a man want to HTTP
Como se llama (si), bonita (si), mi OS (si, Apache Apache), su OS
Apache, Apache
Oh baby when you talk like that
You make IIS go mad
So be wise and keep on
Reading the signs of my Ballmer
And I'm on tonight
You know my ports won't die
And I am starting to feel you boy
Come on lets go, real slow
Don't you see baby asi es perfecto
Oh I know I am on tonight my ports won't die
And I am starting to feel it's right
All the attraction, the tension
Don't you see baby, this is perfection
Apache, Apache
Oh boy, I can see your traffic moving
Half filtered, half cached
I don't, don't really know what I'm doing
But you seem to have a plan
My will and self restraint
Have come to fail now, fail now
See, I am doing what I can, but I can't so you know
That's a bit too hard to explain
Roca en el Internet toda la noche
Roca en el Internet toda el día
Roca en el Internet toda la noche
Roca en el Internet toda el día
I never really knew that it could serve so fast
It makes a man want to HTTP
Como se llama (si), bonita (si), mi OS (si, Apache Apache), su OS
Apache, Apache
Oh baby when you talk like that
You know you got me hypnotized
So be wise and keep on
Reading the signs of my Ballmer
Senorita, feel the conga, let me see you serve like you come from Redmond
Mira en Maryland se baila así, say it!
Mira en Maryland se baila así
Yeah
It's so popular, macro shaft's jealousy a refugee like me back with the Fugees from a buggy territory
I go back like when 'pac carried crates for Humpty Humpty
I need a whole club dizzy
Why the Court wanna break us?
Explorers and Windows
I ain't guilty, it's no monopoly transaction
No more do we snatch ropes
Refugees run the seas 'cause we own our own browser
I'm on tonight, my ports won't die
And I'm starting to feel you boy
Come on let's go, real slow
Baby, like this is perfecto
Oh, you know I am on tonight and my ports won't die
And I am starting to feel it's right
The attraction, the tension
Baby, like this is perfection
No fighting
No fighting
(feat. Office Assistant)
Macros up in here tonight
No fighting, no fighting
We got the fixes up in here
No fighting, no fighting
Apache, Apache
I never really knew that it could serve so fast
It makes a man wants to HTTP
Como se llama (si), bonita (si), mi OS (si, Apache Apache), su OS
Apache, Apache
Oh baby when you talk like that
You make IIS go mad
So be wise and keep on
Reading the signs of my Ballmer
And I'm on tonight
You know my ports won't die
And I'm starting to feel it's right
All the attraction, the tension
Don't you see baby, this is perfection
Hey Girl, I can see your traffic moving
And it's driving me crazy
And I didn't have the slightest idea
Until I saw you serving
And when you take up a bad request
Nobody cannot ignore the way you check it out, girl
And architecture so perfected - the way you right and left it
So you can keep on excelling
I never really knew that it could serve so fast
It makes a man want to HTTP
Como se llama (si), bonita (si), mi OS (si, Apache Apache), su OS
Apache, Apache
Oh baby when you talk like that
You make IIS go mad
So be wise and keep on
Reading the signs of my Ballmer
And I'm on tonight
You know my ports won't die
And I am starting to feel you boy
Come on lets go, real slow
Don't you see baby asi es perfecto
Oh I know I am on tonight my ports won't die
And I am starting to feel it's right
All the attraction, the tension
Don't you see baby, this is perfection
Apache, Apache
Oh boy, I can see your traffic moving
Half filtered, half cached
I don't, don't really know what I'm doing
But you seem to have a plan
My will and self restraint
Have come to fail now, fail now
See, I am doing what I can, but I can't so you know
That's a bit too hard to explain
Roca en el Internet toda la noche
Roca en el Internet toda el día
Roca en el Internet toda la noche
Roca en el Internet toda el día
I never really knew that it could serve so fast
It makes a man want to HTTP
Como se llama (si), bonita (si), mi OS (si, Apache Apache), su OS
Apache, Apache
Oh baby when you talk like that
You know you got me hypnotized
So be wise and keep on
Reading the signs of my Ballmer
Senorita, feel the conga, let me see you serve like you come from Redmond
Mira en Maryland se baila así, say it!
Mira en Maryland se baila así
Yeah
It's so popular, macro shaft's jealousy a refugee like me back with the Fugees from a buggy territory
I go back like when 'pac carried crates for Humpty Humpty
I need a whole club dizzy
Why the Court wanna break us?
Explorers and Windows
I ain't guilty, it's no monopoly transaction
No more do we snatch ropes
Refugees run the seas 'cause we own our own browser
I'm on tonight, my ports won't die
And I'm starting to feel you boy
Come on let's go, real slow
Baby, like this is perfecto
Oh, you know I am on tonight and my ports won't die
And I am starting to feel it's right
The attraction, the tension
Baby, like this is perfection
No fighting
No fighting
Labels:
Apache
Subscribe to:
Posts (Atom)