Training, Open Source computer languages
PerlPHPPythonMySQLApache / TomcatTclRubyJavaC and C++LinuxCSS 
Search for:
Home Accessibility Courses Diary The Mouth Forum Resources Site Map About Us Contact
For 2023 - we are now fully retired from IT training.
We have made many, many friends over 25 years of teaching about Python, Tcl, Perl, PHP, Lua, Java, C and C++ - and MySQL, Linux and Solaris/SunOS too. Our training notes are now very much out of date, but due to upward compatability most of our examples remain operational and even relevant ad you are welcome to make us if them "as seen" and at your own risk.

Lisa and I (Graham) now live in what was our training centre in Melksham - happy to meet with former delegates here - but do check ahead before coming round. We are far from inactive - rather, enjoying the times that we are retired but still healthy enough in mind and body to be active!

I am also active in many other area and still look after a lot of web sites - you can find an index ((here))
Using a character string count

Posted by TedH (TedH), 1 April 2008
Hi all. Forums are a good place to learn stuff and OpenTalk is no exception. As a graphic designer, programming comes hard (really) to me and what would take Graham a few hours to do, takes me months and months. One main aspect is that the input I've received from this forum has led me to think a little differently on how I handle coding problems. This occured to me while tackling some scripting today and so I thought I'd share it as others may find it helpful.

On my (long running - told you I was slow) blog project I have a small but effective search sub-routine. Part of programming is so that viewer can make sense of what they see. I managed to extract various parts of a file path. allowing me to see the month and year during the search result.

The only problem was that I needed to physically count the number of characters in the path to the file, then manually type that into the code. This is a glob with strings and sub strings assigned to variable so they can be printed out, all nice and pretty, for the viewer. As the blog system is going to end up being a freebie to give away I cannot expect the user to do this.

Here's where the thinking differently came in. I realized I needed to separate the path from the filehandle in a character string count.

Each month in the blog has its own, small, flat-file database. The datafolder path is a pre-defined variable (i.e. - /myserver/full/path/datafolder), that's 30 characters.

Here's the before and after:

Before Example, with filename of: 200804.db
The number of path characters had to be coded in by hand.
@dbfiles = glob "$datafolder/*.db";
foreach $mfl (@dbfiles) {
$str = "$mfl";
$str = substr $str,0,+6;
$aa = substr($mfl, 31, 4); ## 31 is number of chars to 2008
$bb = substr($mfl, 35, 6); ## 35 is number of chars to 04
$macvm = $months[$bb-1];
$macvy = $aa;

After Example, with filename of: 200804.db
Counting the number of path characters is handled by the script.
## get character counts for extraction
$ipt = "$datafolder/"; # The / is required.

## ----- This is the character string count.
## ----- It is the basis for my variable definitions.
## ----- I needed an input variable and an output one.
## ----- This is concise and does exactly what I need.
$ipt =~ tr/\000-\xff/./;
while ( $ipt =~ /(.)/g ) {$count++;}
## -------------------------------------- ##

## Refine the count in preparation for the extraction
$foraa = $count; # to the year
$forbb = $count + 4; # to the month

# Extract month and year
@dbfiles = glob "$datafolder/*.db";
foreach $mfl (@dbfiles) {
$str = "$mfl";
$str = substr $str,0,+6;
$aa = substr($mfl, $foraa, 4); ## foraa is variable for chars to 2008
$bb = substr($mfl, $forbb, 6); ## forbb is variable for chars to 04
$macvm = $months[$bb-1];
$macvy = $aa;

The print out from both is the same. Style however you want.
April 2008
print "$macvm $macvy"\n;

Where "macvm" is the month followed by "macvy", the year. Other variable definitions are in a configuration file.

Now I can have a path as short or long as needed and I don't have to worry about touching the code.

The purpose is purely and totally cosmetic. The search works perfectly, on mulitple databases, without it.

All I wanted was the month and year to show over each block (database) of search results.

Again the emphasis for the programming is - will the result be seen by a viewer or not? If not, then you don't have to concern yourself with the cosmetics. However, if someone is going to see the results of your code, then you need to make room for design and layout. Plus - you may have to code in extra stuff to make that happen.

Posted by KevinAD (KevinAD), 1 April 2008
A few notes on the code, not on your perl progression:

Maybe the File::Basename module would have made your life simpler. It extracts a file path into its components. The length() function could have also been used to count the number of characters in a string.

$foo = 'path/to/some/folder';
my $c = length($foo);

You should almost never have to put quotes around single scalars like you did here:

$foraa = "$count"; # to the year

better written as:

$foraa = $count; # to the year

double-quotes are for making strings, you don;t need to make a string out of that scalar, you just need to assign it's value to $foraa. What perl did was made a string out of $count that it never used. There are also subtle problems you can run into using quotes needlessly.

Also, when you have fixed length records, like 200804 you can use unpack() which is even more efficient than substr().

Posted by TedH (TedH), 2 April 2008
Thanks Kevin, I didn't know about the File::Basename Module. It appears to be a standard module with all Perl installations. I'll check it out.

I know my live server has kept this, and I assume all hosting services will, Though there are times hosts remove or change modules (as I've found to my dismay).

The $foraa quotes are gone and it's fine. I didn't know that either. I didn't put any on the $forbb because of the addition.

Cheers - Ted

Posted by KevinAD (KevinAD), 2 April 2008
File::Basename is a core module and has been for a long time. It is true that servers can add or remove modules as they see fit, but any server that removes core modules should be avoided in my humble opinion.

Posted by TedH (TedH), 2 April 2008
Agreed. As what I'm doing is going to be give away from my site (eventually), I have to consider all possibilities - bit of a pain sometimes.

This page is a thread posted to the opentalk forum at and archived here for reference. To jump to the archive index please follow this link.

You can Add a comment or ranking to this page

© WELL HOUSE CONSULTANTS LTD., 2023: Well House Manor • 48 Spa Road • Melksham, Wiltshire • United Kingdom • SN12 7NY
PH: 01144 1225 708225 • FAX: 01144 1225 793803 • EMAIL: • WEB: • SKYPE: wellho