Perl: Using the Date::Calc Module to Generate Date-Related Strings

Saturday, February 8th, 2014

I have found the Date::Calc module to be quite handy on several occasions. It has many, many methods to do just about anything you can think of with dates. If you haven’t used it before, I would suggest you check it out and give it a try.

Today I’m going to show how I recently had to come up with a string that represented the first 3 characters of the current month in all lowercase plus the current 4-digit year. So if this is February of 2014, the string needed to be “feb2014.” The Date::Calc module came in quite handy for this task.

First I start out by including the module in my script:

use Date::Calc qw(:all);

I will get today’s date with the Today() method:

($today_year, $today_month, $today_day) = Today();

Next we will get the actual name of the month (e.g., February). The second parameter here is the language:

$full_mon = Month_to_Text($today_month, 1); #1 = English

We’ll lowercase the month name:

$short_mon = lc($full_proc_mon);

And then we use a regular expression to discard everything after the first 3 characters:

$short_mon=~s/^(.{3}).*$/$1/g;

Now we combine the month and year to get our string (e.g., “feb2014”):

$month_year = $short_mon . $today_year;

Some of these steps could have been combined, of course, but I have broken it out here for ease of reading and debugging.

Have you used the Date::Calc module for any interesting applications? Share your thoughts in the comments.

Perl: Processing a CSV or Tab-Delimited File into a Hash of Hashes

Saturday, December 28th, 2013

Perl is a great language when it comes to processing data. The biggest reason is because its powerful regular expression support makes it easy to find the data you’re looking for.

When you have a file that is in CSV (comma-delimited), tab-delimited, or some other delimited format, Perl is the perfect option for reading in the file and doing something with its data. In this example we will store it into a hash of hashes.

Let’s jump right into the code.

First, we initialize an empty hash where we’ll store the data, then open the file for reading:

%file_data = ();
unless (open (IN, "tab_file.txt"))
{
    print "ERROR: Could not open input file tab_file.txt: $!\n";
    exit;
}

Now we will read in each line in the file:

while ($line = <IN>)
{

Get rid of the ending newline:

    chomp($line);

Here’s the key to getting the data on the line. We’re splitting the line (here on the tab character, but it can be any character) into parts and storing those parts into an array:

    @line_info = split(/\t/, $line);

Let’s say the key field to the line’s information is in the first field, so we store that temporarily:

    $key_fld = $line_info[0];

Now we’re going to iterate through the tab-delimited fields from the line and store them into a hash of hashes for easy retrieval later on. The first line here is converting the array into a reference. The second line is then iterating through that array reference. The line inside the loop stores each field into a hash of hashes: the first key field is the first field on the line, which we defined above, and the second key is just the number of the field. (This might not be the most practical way to store the data, but you get the idea.)

    $line_info = \@line_info;
    for ($i = 0; $i <= $#$line_info; $i++)
    {
        $file_data{$key_fld}{$i} = $line_info->[$i];
    }
}

We close our input file, since we’re done reading it:

close(IN);

Now if we want to iterate through the hash of hashes we created with the data, we can do something like this:

foreach $key (keys %file_data)
{
    print "Key field: $key\n";
    foreach $key2 (keys %{$file_data{$key}})
    {
        print "- $key2 = $file_data{$key}{$key2}\n";
    }
}

And that’s it–we’re done!

Welcome to Perl!

Saturday, December 28th, 2013

This category will contain information about Perl.