I’ll Tell You What…

March 20, 2008

Sink or Swim? Let’s not.

Filed under: Programming — Larry @ 11:21 pm

In the continuing saga that is my professional life and programming, I seem to be doing more in Perl than anything else at the moment. Even with the motivation to read and study Java and Python I seem to be falling back on what I know better. And that’s not a bad thing, mind you. I just think I’m falling victim to the old jump in the deep end before you can swim routine with these other languages.

For the most part, though, I think that it’s good to jump into a project and just flail away at the new APIs, functions and such. But there are other times when trying to learn a language on the side is tedious. Life gets in the way sometimes and the time between picking up the books lengthens. The fundamentals that you read about in the beginning didn’t stick like they should and you find yourself going back and reading it again. That sounds pretty negative but if you think about it we learn a lot when that happens. I had a Murphy’s Law page a day calendar several years ago and one of the days said, “Snider’s Law: Nothing can be done in one trip.” All the things I know really well took a lot of repetition to learn. Sometimes it just takes time and a little patience goes a long way.

There a few big projects that I would like to attack but the foundation just isn’t there and I think it would only disappoint more that educate. For example, I would like to create a Java app that would serve as the central control panel for the the tedious things we have to keep up t0 date at work such as updating the Message of the Day, managing the sudoers file, ssh keys, etc. Not necessarily like the Solaris Management Console but similar, I suppose. Who knows if that project will come to fruition. But, the idea is there and there’s a need so who knows. I like to create and solve problems.

So, where am I now? A little further along in … some direction. And some direction is better than standing/sitting still.

March 17, 2008

BeerSong.pl

Filed under: Programming, Perl — Larry @ 2:27 am
This has a much better output. It actually types out the word for the number instead of just the number itself.

Two bottles of beer on the wall.
Two bottles of beer.
Take one down.
Pass it around.
One bottle of beer on the wall.

One bottle of beer on the wall.
One bottle of beer.
Take it down.
Pass it around.
No more bottles of beer on the wall.

#!/usr/bin/perl -w

use strict;
use Lingua::EN::Inflect qw( NUMWORDS );

my ($beerNumber, $beerNum, @num_word);
my $word = "bottles";

$beerNumber = 99;

while ( $beerNumber > 0 ) {
	$beerNum = wordize($beerNumber);
	
	print "$beerNum " . $word . " of beer on the wall.\n";
	print "$beerNum " . $word . " of beer.\n";
	
	if ($beerNumber == 1) {
		print "Take it down.\n";
		$word = "bottle";
	} else {
		print "Take one down.\n";
	}
	
	print "Pass it around.\n";
	
	$beerNumber--;

	if ($beerNumber == 1) {
		$word = "bottle";
	}
	
	$beerNum = wordize($beerNumber);
	
	if ($beerNumber > 0) {
		print "$beerNum " . $word . " of beer on the wall.\n\n";
	} else {		
		print "No more bottles of beer on the wall.\n";
	}
}

sub wordize {
	my (@beerNumWord, $beerNumReturned);
	
	@beerNumWord = NUMWORDS ( shift @_ );
	$beerNumReturned = ucfirst(shift @beerNumWord); 
	return $beerNumReturned;
}

March 9, 2008

BeerSong.java

Filed under: Programming, Java — Larry @ 1:55 am

/**
 * Simple program that prints out the words
 * to the 99 Bottles of Beer on the Wall
 * song.
 *
 * Based on the BeerSong example in the
 * _Head_First_Java_ book with corrections.
 */
public class BeerSong {
    public static void main (String[] args) {
        int beerNum = 99;
        String word = "bottles";

        while (beerNum > 0) {

            System.out.println(beerNum + " " + word + " of beer on the wall.");
            System.out.println(beerNum + " " + word + " of beer.");

            if (beerNum == 1) {
                System.out.println("Take it down.");
                word = "bottle";
            } else {
                System.out.println("Take one down.");
            }

            System.out.println("Pass it around.");
            beerNum--;

            if (beerNum == 1) {
                word = "bottle";
            }

            if (beerNum > 0) {
                System.out.println(beerNum + " " + word + " of beer on the wall.\n");
            } else {
                System.out.println("No more bottles of beer on the wall.");
            }
        }
    }
}

March 8, 2008

Obligatory First Program

Filed under: Programming, Java — Larry @ 9:25 pm

public class hello
{
    public static void main(String[] args)
    {
        System.out.println("hello, hello...");
    }
}

February 24, 2008

Choices

Filed under: Internet, Networking, etc., Software, Technology, Programming — Larry @ 6:21 am

So I’ve been wondering lately about programming languages.  I’d like to thoroughly learn one but I’m stuck as to which one.  Over the years I’ve used many different languages from a variety of BASICs to Pascal to Perl to Java and so on but I’ve never really sat down to dive in head first and learn it as if my job depended on it.

I don’t really see a future for me using BASIC even though RealBASIC looks really nice given that it’s a cross platform compiler.  If I were writing programs and selling them it would probably be the way to go.  I just don’t see how it would benefit my career any at the moment.

The company I work for uses Ada is many of their programs.  I’ve taken the internal course and like the structure of the language.  In fact, I think that it looks an awful lot like Pascal.  They appear to share various key words and such.  But as I look at the newer programs and the requirements in the job market I
just don’t see myself going there.

I took a Fortran class in college and received the dubious honor of being the only one in class that thought the language looked like BASIC.  My instructor took offense to my half-joking comments but I had an almost perfect score in the class so he didn’t say much about it.  :)   That was 16 or 17 years ago (yikes!) and I never used the language outside the class so I think it’s safe to say that Fortran is off the list.

PHP is just a hobby language for me because the only thing I’ve used it for is my own website.  It’s not used hardly at all at work.  I did make a file browser-type application for some friends but that’s about it.  There are several things I like about the language.

I almost forgot my OS/2 days and REXX.  The only thing worth while I did with REXX was to write a desktop background changer that stored the history  in extended attributes.  I thought it was cool.

A few languages that I’ve looked into but never really got very far (disclaimer: I have studied them enough to perform code walk-throughs while providing meaningful input): C, C++.

This just leaves Perl, Python and Java.  Of the three, I have the most experience with Perl.  In fact, my employer has sent me to classes to study the subject.  We have a ton of home-grown applications written in Perl.  The most complicated program I’ve written is server-client application that checked with NIS to check on password expiration on successful user log in.  There are plenty of opportunities to make use of Perl expertise but they would be limited to server maintenance, which isn’t all bad I assure you.  Is this skill portable?  As a sys admin, yes but there just aren’t that many jobs needing Perl programmers.

There’s not much Python expertise where I work, unfortunately, but there are some groups that use Mentor Graphics and Python appears to be integrated fairly well.  I bought a book to learn Python a while back and have been slowly reading through it and trying it out.  I really like the structure and object oriented code that’s used.  What I really like is the idea that one would code for maintainability and readability!  I know a Perl programmer or two that write code at such a level that it can’t hardly be maintained at all.  I look at the code and think that it’s overly complicated.  To be honest, I just wanted to check it out to see why so many Perl guys hate Python so much.  As I search around the Internet there appears to be more Python programmer positions that Perl but there’s still not a huge demand for them.

A few years back (5 or 6), I was in between projects at work and thought that I might learn Java since I had heard that an upcoming major contract was going to need Java programmers.  It was web-based and pseudo-interactive with a somewhat limited amount of knowledge to convey.  However, it did whet my appetite concerning objects and GUI programming.  I think I spent 2 months studying Java but, in spite of my enthusiasm and constant nagging, my manager never approached the software engineering group about a job. Being too good can prevent you from expanding your horizons, evidently.  Anyway, the whole episode just depressed me and I trudged on like a good corporate robot.  That was then.

The reason I’m even bringing it up now is that the IT group in my division is laying a few folks off across the corporation (50 total) and even though I’m not on “the list” I’ve been looking anyway.  What I’ve found is an over abundance of Java positions and some of them are fairly high paying jobs.  In fact, some of them even require a certification such as CISSP of which I am a holder.  :)

I think for now I’ll just do some Python and Java reading with the hope that one of them will jump out at me and say, “I’m the one you’ve been looking for.”  We’ll see what happens.

August 22, 2005

Verse of the Day - ver 0.5

Filed under: Bible, Software, Programming, Perl — Larry @ 7:08 pm
Here's a very basic verse of the day-type application that I wrote while playing with Perl and the Tk module. This version is platform independent, ie, it works with Windows, Linux, Solaris, Mac OS X, etc. In order to run this under Windows, you must have ActiveState Perl installed and have the Tk module installed. It may already be installed. For Mac OS X, X11 needs to be running with Perl and the Tk module installed. For all other *NIX platforms, Perl needs to be there as well as the Tk module.
#!/usr/bin/perl -w

use strict;
use LWP::Simple;
use XML::Simple;
use Tk;
use Text::Wrap;

my ($content,$ref,$verse,$title);
my ($passage,$mw,$button,$column);

$Text::Wrap::columns = 40;

# Uncomment and modify the next line if you use a proxy
# $ua->proxy('http', '');
$content = get("http://www.gnpcb.org/esv/share/rss2.0/daily/")
	or die "Couldn't get URL: $!\n";

$ref = XMLin($content);

$verse   = $ref->{channel}->{item}->{description};
$title   = $ref->{channel}->{description};
$passage = $ref->{channel}->{item}->{title};

$verse = wrap('', '', $verse);

$mw = new MainWindow(-background => "#FFFFFF");
$mw -> title($title);

$mw -> Label(-background => "#FFFFFF",
             -text       => "$verse\n\t\t$passage\n")
     -> pack(-ipady      => "10",
             -ipadx      => "30");

$button = $mw -> Button(-text    => "Quit",
                        -command => sub { exit; } ) 
                -> pack(-expand  => "1",
                        -fill    => "x",);

MainLoop;

March 19, 2005

Fun with Numbers

Filed under: Technology, Programming — Larry @ 11:17 am
An interesting question is on the Google Labs Aptitude Test. Question 17 asks the following:
17. Consider a function which, for a given whole number n, returns the number of ones required when writing out all numbers between 0 and n. For example, f(13)=6. Notice that f(1)=1. What is the next largest n such that f(n)=n?
I thought about it for a while but since I couldn't figure out a way in my head to get the answer I thought, "I could program that." I initially wrote it in PHP because I've used it quite a bit here lately. I chose to count up to 2,000,000 so I could time the execution.

#!/usr/bin/php

$ones_count = 0;
		
for ( $i = 1; $i <= 2000000; $i++ ) {
    $ones_count = $ones_count + substr_count( $i, "1" );
	
    if ( $ones_count == $i ) 
        echo "$ones_count == $i \n";
}	
And then I thought, "How fast would this be if it were written in Perl?" This is what I came up with.

#!/usr/bin/perl

my $ones_count = 0;

for ( $i = 1; $i <= 2000000; $i++ ) {
    my $pos = 0;

    while ( ( $pos = index( $i, "1", $pos )) != -1 ) {
        $ones_count++;
        $pos++;
    }

    if ( $ones_count == $i ) {
        print "$i == $ones_count\n";
    }
}
The computer I tested this on is a PowerMac G5 Dual 2.0 GHz with 2 GB RAM. Here are my benchmarks: PHP on average took about 6.3 seconds. Perl, on the other hand, on average took about 5.1 seconds. I didn't even think my Perl code was very efficient either. Of course, I'm not a programming guru at all so if you know of a way to optimize the code let me know.

32 queries. 1.968 seconds. Powered by WordPress