Using a character string count
Posted by TedH (TedH), 1 April 2008Hi 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.
After Example, with filename of: 200804.db
Counting the number of path characters is handled by the script.
The print out from both is the same. Style however you want.
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 2008A 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 2008Thanks 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 2008File::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 2008Agreed. 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 www.opentalk.org.uk and archived here for reference. To jump to the archive index please follow this link.
PH: 01144 1225 708225 • FAX: 01144 1225 793803 • EMAIL: email@example.com • WEB: http://www.wellho.net • SKYPE: wellho