The best magazine
Linux Newbie Administrator Guide -8.2 Simple Programming under Linux
filename: ";
chomp($infile=<STDIN>) ;
open(INFILE,"<$infile") ; # open the file for reading.
print STDOUT "Enter the number of initial lines to preserve: ";
chomp($iskip=<STDIN>) ; # the first lines may contain column headings etc
print STDOUT "Enter the skip: ";
chomp($skip=<STDIN>) ;
#
# The name of the output file is created automatically on the basis of the
# input file and the selection of lines. It is always of type CSV, so preserve is so.
$outfile=$infile.'-pro'.$iskip.'-'.$skip.'.csv'; #glue strings together using the dot operator.
open(OUTFILE,">$outfile") ; # open file for writing.
#
# write the "initial" lines to the output file.
for($a=0;$a<$iskip;$a++) {
$line=<INFILE>;
print OUTFILE $line;
}
#
# do the rest of the file
$c=0;$w=0;$skip++;
while($line=<INFILE>){
$c++;
if(!($c%$skip)) { #use % for remainder of integer division
print OUTFILE $line;
$w++;
}
}
#
close(OUTFILE) ;
print STDOUT "Read Lines: ", $c+$iskip," Wrote lines: ", $w+$iskip,"\n";
8.2.2 Python
python
Modern and very elegant object oriented interpreter. Powerful and (arguably) more legible than perl. Very good (and large) free handbooks by G. van Rossum (the Python creator) are available on the net (try: http://www.python.org/doc/ for browsing or ftp://ftp.python.org for downloading).
How do I write a simple Python program?
Edit a text file that will contain your Python program. I can use the kde "kate" editor to do it (under X):
kate try_python.py &
Type in some simple python code to see if it works:
#!/usr/bin/env python
print 2+2
The first line (starting with the "pound-bang") tells the shell how to execute this text file--it must be there (always as the first line) for Linux to know that this particular text file is a Python script. The second line is a simple Python expression.
After saving the file, I make it executable:
chmod a+x try_python.py
after which I can run it by typing:
./try_python.py
Python is an excellent, and very modern programming language. Give it a try, particularly if you like object oriented programming. There are numerous libaries/extensions available on the Internet. For example, scientific python (http://starship.python.net/crew/hinsen/scientific.html) and numeric python (http://sourceforge.net/projects/numpy) are popular libraries used in engineering.
Here is a slightly longer, but still (hopefully) self-explanatory python code. A quick note: python flow control depends on the code indentation--it makes it very natural looking and forcing legibility, but takes an hour to get used to.
#!/usr/bin/env python
# All comments start with a the character "#"
# This program converts human years to dog years
# get the original age
age = input("Enter your age (in human years): ")
print # print a blank line
# check if the age is valid using a simple if statement
if age < 0:
print "A negative age is not possible."
elif age < 3 or age > 110:
print "Frankly, I don't believe you."
else:
print "That's the same as a", age/7, "year old dog."
8.2.3 tcl
tcl
(Pronounced "tickle".) Popular scripting language.
A simple tcl program?
#!/usr/bin/tclsh
puts stdout {Hello World!}
wish
(type in X-terminal ) A front-end to Tk, an X-windows extension of tcl. Often used for building front-ends of a program.
How do I write a simple GUI program (using Tk)?
Tk is a GUI extension of the easy yet powerful tcl programming language. For example, I may use pico to create a text file that will contain a simple tk program:
pico try_tk
and type in a simple example of tk code to see if it works:
#!/usr/bin/wish
button .my_button -text
Source: ...