Archive for February, 2010

Ubuntu Forum beginners challenge #9

February 21, 2010

I wrote the following short program as a response to a ‘beginners programming challenge’ on the Ubuntu forum. I just got so tired of all the terminal-based solutions and chided the contestants that even phones had GUIs these days! It was kind of difficult separating original entries from rehash/error-correction but the tallies (by the time enthusiasm seems to have got lost) was:

PHP 11
C 4
Java 4
Python 4
Bash 2
C++ 1
Gambas 1
Haskell 1
Lisp 1
OO Spreadsheet 1
Perl 1
Scheme 1
Userland/awk 1

I didn’t see much evidence of ‘beginners’ in the entrants.

Anyway the brief was:
Your program should be able to open a text file and read its contents. The file will consist only of LOWERCASE letters and numbers 0-9. The file will be formatted such that there is only one alphanumeric character per line.
Your program must read each line and store the data in a data structure of your choice. Parse through your structure and print out the following information:

1. The sum of all the individual digits.
2. How many times each character appeared in the file.

I have put the code in a page on the right. It just needs a few standard objects on the form: a filechooser (default name filechooser1), a listbox (listbox1), a button named btnExit and a button named btnRun. If any real ‘beginner’ needs help to get this going, just shout.

It is easy enough to cut and paste from the blog although I have also put the source code as a tar.gz at Prog Challenge Source.

Gambas also does a good job at compiling run-time versions of applications and I have put a Debian package installer at Prog Challenge Installer.

Gambas FTPClient

February 7, 2010

FTP (File transfer protocol) is used to send and receive files over the internet. I have issue with a lot of the supplied Gambas examples in that they often over-complicate the process they are trying to demonstrate. New users, searching for the answer to a particular problem, need the bare-bones answer: something on which they themselves can then build. Searching on ‘Gambas’ and ‘FTP’ currently leads to samples of amazing complexity with timers and event-logs all over the place to the point where you just can’t get the essence of the sample itself.

Well here is the bare-bones of FTP in Gambas:

‘ Gambas class file
PRIVATE myFTPClient AS NEW FtpClient

PUBLIC SUB Button1_Click()

myFTPClient.URL=”ftp.drivehq.com/Documents/FileToSave.txt”
myFTPClient.User = “fred”
myFTPClient.Password = “dog”

myFTPClient.Put(“/home/fred/FileToGo.txt”)
END

Now, how simple is that? The .URL includes the file name on the ftp server and the .Put gives the local filename that you want to send. You won’t be surprised to hear that there is a .get method to do it the other way round.

Ideally you would do a bit more e.g. monitor theFTPClient.status to check completion.

Make sure you click on networking components when you create the project (or click on gb.net and gb.net.curl in project,properties,components)