i.e. some stuff and junk about Python, Perl, Matlab, Ruby, Mac X, Linux, Solaris, ...
Saturday, December 16, 2006
VNC server on Mac classic (8.6)
I decided to try to install VNC server on Mac classic and try how it goes.
I installed chromivnc server 3.40a5. The only thing after the installation, or rather I should say unpacking, was to copy vncPatches file to Systems Folder/Extensions and restart my mac. Afterwards, I started vnc server without any problems, and that was it. My vnc server on Mac 8.6 was working.
Nevertheless, I have to admit, that it is considerably slower with comparisons to more newer version of vnc for Mac X, or Linux. However, it is enough for fast, small work on Mac OS 8.6.
Below image showing Kubuntu with VNC connection to Mac X Tiger and Mac OS 8.6
Friday, December 15, 2006
VNC client on Mac classic (8.6)
Finally, recently I found solution. It is simply installing VNC server on my Mac X 10.4 and installing VNC client on Mac 8.6 and using this client to work on Tiger, while being on Classic.
Nonetheless, finding and starting VNC client on Mac Classic was not so simple. After some searching and reading I found what I was looking for, and I would like to shear with it. Hopefully it will be useful for someone :-)
VNC client on Mac Classic is VNCThing 2.2 to download e.g. from here.
Morever, to run this client it is necessary to have
CarbonLib 1.6 from here.
After installation of those programs I could enjoy my Mac X 10.4 from Mac 8.6.
Below to screen showing Mac 8.6 with VNC client connected to Mac X Tiger.
Tuesday, December 05, 2006
Ruby: Using DarwinPorts' Ruby instead of Mac X's
sudo port install ruby
.Unfortunately after the installation you will have two Ruby's in your systems, and your xterm console by default will use the first one. In order to use second one, the PATH environmental variable must be slightly change i.e. putting directory
/opt/local/bin
(DarwinPorts default directory) before every other directory in PATH. As a results programs in /opt/local/bin
will have priority over the other programs.To change the path you can execute just:
export PATH=/opt/local/bin:$PATH
Sunday, November 26, 2006
Ruby: one liners
-e 'command'
and -n
, where:-
-e 'command'
- Executes command as one line of Ruby source. Several e’s are allowed, and the commands are treated as multiple lines in the same program. If programfile is omitted when e is present, execution stops after the e commands have been run. Programs 1.8 run using e have access to the old behavior of ranges and regular expressions in conditions—ranges of integers compare against the current input line number, and regular expressions match against $_. -
-n
- Assumes a while gets; ...; end loop around your program.
For example simple grep program can be implemented in Ruby as:
ruby -n- e "print if /blabla/" *.txt
Or, for instance, if one wants to save a list of all *.tif, *.TIF, *.tiff, *.TIFF files in given directory in lowercase:
ls -l | ruby -n -e ' puts split[8].downcase if $_.to_s.upcase =~/TIFF?/' > list.txt
Of course, those examples can be done without the need of Ruby, just by using standard Unix command, but sometimes it is easer to use e.g. Ruby (the same thing can be done using Perl, Python, awk,...) than to look for syntax of a given command. If one is familiar with e.g. Ruby, few separate Unix commands can be done in one Ruby command-line program.
Many other handy examples of ruby one liners can be found here.
Tuesday, November 07, 2006
Firefox 2.0: Session Restore
For example, one of many websites that crashes Firefox 2.0 is http://www.sf.airnet.ne.jp/~ts/japanese/vowels.html . I do not know why, but I know that when it happens I have to use Safari to look for those 'bed' sites. Of course I do not say it is in general and true for every user of Firefox 2.0, I assume that it is only my Mac that has problem with this one website.
Despite this problem with some pages, it could be worse. The fact that it is not so totally bed, is new option in second edition of Firefox called Session Restore. From Firefox web side:
Now, with Session Restore, if Firefox has to restart or closes when it comes back you’ll pick up exactly where you left off. The windows and tabs you were using, the text you typed into forms, and the in-progress downloads you had running will all be restored. You can even set Firefox 2 to always restore your previous session instead of loading a home page, so you’ll never lose your place again.
And this is nice. Even thought my Firefox crashes, I do not loose all tabs and most importantly I do not have to go to pages again which sometimes may be difficult if you have many tabs open. I have them automatically restored.
Sunday, November 05, 2006
Matlab: Lack of multithread
to write image processing algorithms in Matlab 7.0, which are usually very time consuming and require wast amount of resources.
Despite the power of this machine, I cannot use it fully due to Matlab. This is because by definition it is only singe threaded application and there is on way of creating few threads. This of course causes that even thought given algorithm or application could be divided to e.g. four parallel threads, it cannot be done due to Matlab.
This drawback of Matlab is really frustrating, because Matlab uses fully only one processor out of four. On the other words, while one processors is fully loaded, three others do not do anything useful. As a result of that, my computations take four times more time to finish, compared with the situation if I could use all four processors in Matlab.
For instance, sometimes I write something in ANSI C, and there is no problem of creating threads; hence, my algorithms in ANSI C are four time faster than the same single threat algorithms on this Sun server, because they use all four processors.
Nevertheless, using single thread Matlab on four-processors server has some advantage. The main advantage of this is that I can run four separate Matlab instances, which I do often. For example, if I have one thousand images to process, I can run four matlab in four consoles, and I can tell that the first instance processes images from 1 to 250, second one processes 251 to 500, third and forth one 501-750 and 751 to 1000 respectively.
Moreover, I can run four Matlabs processing 1000 images each, but with different parameters. Consequently I can check my program with four different parameters and check their impact on the results simultaneously, rather than execute one version of program by another. This also uses four processors and I can do my work four times faster than on single Matlab instance.
Thanks to this, even though my application is single threaded, I can process 1000 images four time faster using four Matlabs than using one Matlab. This way, 100% of computation power of my server is used, and all four processor are doing something useful.
Below I present screen from top command, showing four Matlabs using in summary almost 100% of CPU power of my Sun.
Thursday, November 02, 2006
Mac X: Virtual desktops
I think I do not have to explain what virtual desktops are. Everyone who has every used Gnome or KDE in Linux should know it. The only thing I can say is that they are very, very convenient because:
They increase a user's ability to organize their windowed applications that are currently running by reducing clutter. - wikipediaLack of virtual desktops systems (free ones of course) on Mac X 10.4 used to annoy me a lot, because I use considerable amount of windowed application everyday at my work. Consequently, few months ago I started to look for some freeware application that provides virtual desktops for Intel iMac with Mac OS X 10.4. Nevertheless, at the time the only free program that I found did not work on Tiger - after starting it, my Mac just freezed. Unfortunately I do not remember the name of that program, or even where did I take it from.
Recently, due to sneak peak of Leopard I decided to give it one more try and to look for some free virtual desktop manager working on Tiger. And for my big surprise I have found it. It is called just Desktop Manager and its official site is here. I downloaded DesktopManager-0.5.3.dmg and I installed it and it worked!!!.
Thanks to this little program now I use six virtual desktop which is great. Finally I can open as many window application and consoles as I want, and I can manage through them very easily.
Bellow some pictures of my Mac and transition between desktops. Sorry for the quality, but I had to use digital camera to take them, because normal screen shots do not work with this.
Wednesday, November 01, 2006
Mac X: Ogg files and iTunes
Nevertheless I did not wanted to use Whamb - I just dont not like it - hence, the only choice I had (and the fastest because at the very moment I did not have time to struggle with that) was to decode my *.ogg files to mp3 files. To to this I had to use
oggdec
program which gave me wave files, and than using lame
encoder I converted those *.wav files to mp3. Of course prior to this, I had to install all necessary stuff like oggdec
and lame
.I wander why Apple does not support ogg? I thought it was free open source format?
Gimp: Changing language back to english
As usually in such cases I did a little of web searching and I found some solutions. First proposed solution was to set LANG environment variable to 'en'. Of course I tried it, and of course it did not work.
Second solution that fortunately worked was to delete folder containing Japanese language in Gimp's directory. Hence, I deleted folder ja from:
After restarting Gimp I had it finally in English.
In conclusion, as far as my case is considered the solution to Gimp language problems is to delete given lang folder from Gimp.app directory.
Sunday, October 29, 2006
Measure program execution time
time
command. For example
>time ls
>real 0m0.006s
>user 0m0.001s
>sys 0m0.004s
Command
time ls
measures time used by ls
program.
Saturday, October 28, 2006
Matlab: Working with text data files using Perl (2)
Former program, reading rectangles.txt file can be of course written in Matlab, without any Perl help. My example with rectangles.txt is quite simple as far as text file is concerned; nevertheless, I used Perl to read and search this file in order to show how Perl can be used within Matlab.
Below I present Matlab code, that does exactly the same task as Perl script presented in my former post.
function [rect_1 rect_2 point]= getRects(filename)
rect_1=[];
rect_2=[];
point=[];
fid=fopen('rectangles.txt');
while true
tline = fgetl(fid);
if tline(1)=='#', continue;end;
if ~ischar(tline), break, end;
[file,theRest] = strtok(tline);
if ~strcmp(file,filename), continue; end;
RECTs= sscanf(theRest,'%f')';
rect_1=RECTs(1:4);
rect_2=RECTs(5:8);
point=RECTs(end-1:end);
end
fclose(fid);
The question now is which method (Matlab+Perl or Matlab only) is faster?
To check it I used
tic;getRects('105aty.tiff'); toc;
combination in Matlab both for full Matlab script and Matlab/Perl script. Each script was tested 100 times.Matlab only version : 0.18 (std=0.03) sec
Matlab+Perl version: 0.10 (std= 0.02) sec
It is clearly seen that connection Matlab+Perl to work with text data is considerably faster
that of Matlab only.
Appendix:
Test was made on:
Matlab 7.0.0 (R14) and Perl 5.6.1 running on Sun Fire V440 Server.
std - standard deviation
Friday, October 27, 2006
Matlab: Working with text data files using Perl (1)
Nevertheless, there are few things that are easy and natural in those other languages, whereas in Matlab they become quite difficult. What really annoys me, and not only me, are: (i) lack of hash arrays, (ii) working with text files and (iii) lack of multi-thread work.
Factors (i) and (ii) are closely related. Everyone who has ever used Perl, Ruby or Python to work and mine text files knows that hashes are essential. Of course, they are useful to many other things, however based on my own experience, vast majority of operation of files were based on hash arrays usage. What is more, I have to say, that you can easily work with Matlab without hash tables, and I really did for a long time. But, when recently I needed to process (write/read/create/joining data from few files/extracting and looking for string and numeric data) text files in Matlab, lack of hash tables, and poor file handling operation in Matlab become very frustrating.
Fortunately there is Perl, which is excellent in dealing with text files. However, the most important thing about this, is the fact that Matlab has embedded support for using Perl scripts within Matlab programs using
perl
function:Perl is included with MATLAB on Windows systems, and thus MATLAB users can run M-files containing the Perl function. On Unix systems, MATLAB just calls the Perl interpreter that's available with the OS - from Matlab help.This function can save a lot of work, and make working with files in Matlab very fast, and convenient; in fact, as convenient as Perl is. At the same Matlab page we read:
It is sometimes beneficial to use Perl scripts instead of MATLAB code...Indeed, it is very beneficial, especially if one works for example with text files and further in this post I am going to show one example how Perl can be used withing Matlab while working with txt files.
1. Data File
My test text file is called rectangles.txt. It contains file names (first column) and description of two rectangles and one point. Each rectangle is characterized by four values: [x y width height].
First rectangle is described in columns 2-6, second in 7-10. Two last colums (11,12) contain
position [x,y] of a point.
2. My task
Little script in Matlab (getRects('filename')), which takes file name, searches given file name in the file, and returns three vectors ( rect_1=[x y width height]; rect_2=[x y width height]; point=[x y]) based on info in the file, for instance:
[rect_1 rect_2 point] = getRects('001anv.tiff');
%GIVES:
rect_1 = [113 302.5 49.035541 58.514286];
rect_2 = [274 292.5 90.87837491 89.96484776];
point = [5.220703 4.375];
3. Method
To do my task, I will create simple Perl script, that will take file name form Matlab, search for data, and returns required vectors to Matlab.
4. Matlab script
Lets start from creating our Matlab script.
function [rect_1 rect_2 point]= getRects(filename)
%execute Perl script
a=perl('getRectPos.pl',filename,'rectangles.txt');
eval(a); %evaluate string returned from Perl
As can be seen the script is very easy. It just executes Perl program passing two arguments to it: (i) filename to be search in the file; (ii) 'rectangles.txt' - name of file to look from.
The interesting fact about this is the
eval(a)
operation. As described in my former post, Perl returns string representing its standard output. Hence, in order to get Matlab data structures and variables from Perl, it is required to return them as strings, and then execute that string using eval
function.5. Perl script
Finally, we come to the Perl script it self.
#File: getRectPos.pl
#Read arguments from Matlab
my $fileName = $ARGV[0];
my $fileWithRect = $ARGV[1];
#create piece of Matlab code - declaration
#of variables.
my $out="rect_1=[]; rect_2=[]; point=[];";
my %goldHash=&readFileToHash(
$fileWithRect
);
if ( exists $goldHash{$
fileName
}) {
my @r1 = @{$goldHash{$key}}[0..3]; #(x,y,w,h)
my @r2 = @{$goldHash{$key}}[4..7]; #(x,y,w,h)
my @p = @{$goldHash{$key}}[-2..-1]; #(x, y)
#create the rest of the Matlab code to be executed by eval
foreach my $v (@gmROI) { $out .= "Med(end+1)= $v; "; }
foreach my $v (@glROI) { $out .= "Lat(end+1)= $v; "; }
foreach my $v (@scales) { $out .= "Scales(end+1)= $v; "; }
}
#print string to Matlab
print $out;
sub readFileToHash {
#read file values to a hash table
#key value in this hash is file name (first column)
#data is all the rest columns: 2-12
my $fname = shift;
my %temp=();
open(FG,"<$fname") || die "Cannot find file ",$fname; while () {
chomp;
my ($k,my @rec) = split('\t',$_);
$temp{$k}=[@rec];
}
close FG;
return %temp;
}
6. Results
After executing Perl script, Matlab gets string to variable a. Example content of that string is:
a=
rect_1=[];rect_2=[];point=[];rect_1(end+1)= 104.000000; rect_1(end+1)= 303.000000; rect_1(end+1)= 49.035541; rect_1(end+1)= 58.514286; rect_2(end+1)= 282.964459; rect_2(end+1)= 294.000000; rect_2(end+1)= 49.035541; rect_2(end+1)= 58.514286; point(end+1)= 5.220703; point(end+1)= 4.375000;
after executing
evel(a)
, in Matlab environment all required vectors are created (rect_1,rect_2,point).7. Conclusion
Thanks to Perl and possibility of using it within Matlab many task concering text files can be solve more easily and more convenient.
Tuesday, October 24, 2006
Firefox 2.0 out now
After maybe two minutes of use, I have to say that one thing about this browser is especially useful for me, and I think for most of the Internet users. I am talking of course about build in Spell Checking which is just great. Now I'm writing this fast post, and Firefox is looking after my spelling. It is much more convenient than using ABS button. Additionally I checked if there are any others languages in spell checking than English, and of course there are plenty of them, including my home one - Polish.
The next two features that are very seen instantly is refreshed look, and improved Tabbed Browsing. Now, each tab has its own close button, which is, I think, better than before.
Next, very useful new thing about Firefox is so-called Search Suggestions. Starting typing in search bar, gives a list of suggestion from search engines. For example from Google or Answers.
Of course there are many more new features than those for example: Session Restore, Live Titles, Live Bookmarks, Streamlined Interface, Phishing Protection, Protection from Spyware ..... List of all is here.
Postfix: message_size_limit
message_size_limit = 20240000
This increased my mail size to around 20MB.
Perl: Own module with my common functions
I dicided to do my own package, and here I will present some more or less what I have fastly done to get what I wanted.
First I create module. The code goes in the file named MyModule.pm within the directory Utilities: that is, Utilities/MyModule.pm
- package Utilities::MyModule;
- use Exporter;
- @ISA = qw(Exporter);
- @EXPORT = qw(&myFun1 &myFun2);
- sub myFun1 {
- print "Some action of myFun1";
- }
- sub myFun2 {
- print "Some action of myFun2";
- }
- 1;
Line 4 assigns the list
(&myFun1 &myFun2)
to the special, per-package array @EXPORT
. When someone imports this module, variables and functions listed in that array are aliased into the caller's own package. That way they don't have to call the function Utilities::MyModule::
myFun1()
after the import. They can just write shuffle(23)
instead. This won't happen if they load Utilities::MyModule with require
Utilities::MyModule; only a use
imports.Lines 5 and 8 set up the package global functions to be exported.
1
, indicating the overall return value of the module. If the last evaluated expression in the module doesn't produce a true value, an exception will be raised.After creating this module, you can import it simply by:
use Utilities::MyModule;
Matlab: java.lang.OutOfMemoryError
Java exception occurred:
java.lang.OutOfMemoryError: PermGen space
So I did a little of the Internet browsing and I found out that the solution to that problem was to create file java.opts in my working directory. In this file, one can put all options to java virtual machine. To increase my memory amount i had to put there:
-Xms128m
-Xmx128m
-XX:PermSize=128m
-XX:MaxPermSize=128m
Only with that option, my Matlab 7.0 and my java program worked. I also tried solution at official matlab site: here. But it didn't work. I had to use all four parameters in order to run my program.
Xemacs and mouse scroll on Mac X and Solaris
The solution to my problems was to add to the end my Xemacs configuration file (Users/my_user_name/.xemacs/init.el) following lines:
(define-key global-map 'button4
'(lambda (&rest args)
(interactive)
(let ((curwin (selected-window)))
(select-window (car (mouse-pixel-position)))
(scroll-down 5)
(select-window curwin)
)))
(define-key global-map [(shift button4)]
'(lambda (&rest args)
(interactive)
(let ((curwin (selected-window)))
(select-window (car (mouse-pixel-position)))
(scroll-down 1)
(select-window curwin)
)))
(define-key global-map [(control button4)]
'(lambda (&rest args)
(interactive)
(let ((curwin (selected-window)))
(select-window (car (mouse-pixel-position)))
(scroll-down)
(select-window curwin)
)))
(define-key global-map 'button5
'(lambda (&rest args)
(interactive)
(let ((curwin (selected-window)))
(select-window (car (mouse-pixel-position)))
(scroll-up 5)
(select-window curwin)
)))
(define-key global-map [(shift button5)]
'(lambda (&rest args)
(interactive)
(let ((curwin (selected-window)))
(select-window (car (mouse-pixel-position)))
(scroll-up 1)
(select-window curwin)
)))
(define-key global-map [(control button5)]
'(lambda (&rest args)
(interactive)
(let ((curwin (selected-window)))
(select-window (car (mouse-pixel-position)))
(scroll-up)
(select-window curwin)
)))
This is what I was looking for, and what's the most important - it worked.
In addition to that, I did exactly the same thing to my XEmacs on my Solaris server (I use X11 to work remotely on Solaris from my Mac). It worked also.
Ruby, intersection of two arrays of objects
Ruby comes with overloaded operator &, using which intersection of two array is childish.
For example:
#!/usr/bin/env ruby
a=[1, 2, 3, 4]
b=[1, 2, 2 ,3 ,4, 5, 6]
c= a & b #intersection of 'a' and 'b' arrays
print c
will produce: 1 2 3 4
And for example this:
#!/usr/bin/env ruby
a=['aa', 'ac', 'acb', 'acbd']
b=[ 'ac', 'acb', 'bca', 'zwx']
c= a & b #intersection of 'a' and 'b' arrays
puts c
will produce: ac acb
Everything is OK, when you perform intersection of standard types, like String or Integers, etc.
However, there is problem when you create your own class, and try to perform intersection of array of objects of your class.
#!/usr/bin/env ruby
class MyClass #my own very simple class
attr_reader :var1, :var2
def initialize(arg1,arg2)
@var1 = arg1
@var2 = arg2
end
def to_s
"#{var1}: #{var2}"
end
end
a = [MyClass.new('house',3), MyClass.new('mouse',5)]
b = [MyClass.new('cat',3), MyClass.new('house',5), MyClass.new('mouse',5)]
c = a & b #INTERSECTION !!!
puts c
This, unfortunately gives nothing!
After a while I found solution to that. The solution is: you must overload eql? and hash functions in you class. Java programmers should now something, because as I remember in Java, you do something similar while working especially with Comparable interface.
So, to make intersection, final example my program looks like this:
#!/usr/bin/env ruby
class MyClass attr_reader :var1, :var2
def initialize(arg1,arg2)
@var1 = arg1
@var2 = arg2
end
def to_s
"#{var1}: #{var2}"
end
def eql? other #OVERLOADED eql? !!!
other.kind_of?(self.class) && @var1 == other.var1
end
def hash #OVERLOADED hash !!!
@var1.hash #use var1 hash value,
#as hash for MyClass
end
end
a = [MyClass.new('house',3), MyClass.new('mouse',5)]
b = [MyClass.new('cat',3), MyClass.new('house',5), MyClass.new('mouse',5)]
c = a & b #INTERSECTION !!!
puts c
house: 3
mouse: 5
I used only standard hash value from @var1 in my class, because I was only interested in making intersection based on @var1, nevertheless there is noting against to make more complex hash and eql? function.
NFS sever on Ubuntu 5.10
To setup nfs server on you Ubuntu, few steps are required.
- Install packages sudo apt-get install portmap nfs-common nfs-kernel-server
- Create, if not exists, file: /etc/exports where you indicate folders you want to share. For example I have in my exports (IP addresses have been change):
/home/me/mynfs 196.17.227.221/255.255.255.0(rw,sync,all_squash)
The above means I want to export the /home/me/mynfs directory to computer 196.17.227.221 with the options "read&write" &"sync"&"all_squash". - Start/restart you nfs server: /etc/init.d/nfs-kernel-server start
showmount -e localhost
and if you see:
Export list for localhost:
/home/mwolski/mynfs 196.17.227.221/255.255.255.0
it means it work. However, there can be little problem caused by firewall. I had to enable all connection to port 2041, to allow external users to connect to my share. To chack if you can mount this share from other computer, just run showmount -e IP_OF_NFS_server.
To run NFS server it is enought.
Later I will descripe how to mount nfs share and also I try to figure out all those nfs options, you can put to /etc/exports.
Monday, October 23, 2006
Finally my blog is live again
Finally, after few months of absence my blog returns to the Web. Unfortunately, my former blog is down, due to server problem; hence, I decided not to wait longer and to sing up for google blog.
First thing, that I have to do, is to recreate my posts from my former blog. It should not take log. Afterwards, I will put some new content. Hope you will enjoy.
Friday, October 06, 2006
Mac X 10: How to make screenshots
1. Basic
2. Using Grab program
Basic
To capture entire screen you press Command-Shift-3 and the Picture 1.png image file is created on your desktop. You can also use Command-Shift-4 to select only part of your screen, and just like before Picture.png file will be created. On the other hand using Command-Shift-Control-4 instead of file, the screen is copied to the clipboard.
Using Grab program
This program is in Applications -> Utilities folder.
I personally prefer the Basic way of doing screen shots. For me it is more convenient. Moreover I prefer png files over tiff files, which are created by Grab.
Sunday, September 24, 2006
Perl: File handle as function argument
So, let me just give a solution to this problem, without any further explenation.
#!/usr/bin/perl
use strict;
open(FH,"> some_file.txt");
print FH "Write first line to the file";
writeToFile(*FH);
sub writeToFile {
my $FH = shift;
print $FH "Write second line to the file";
}
Hence, one must remember that when we past a file handler to a function in Perl, we must do it using '*', no other way that I know of works.
Ruby, is it worth my interest?
Ruby, as its homepage says, is: "the interpreted scripting language for quick and easy object-oriented programming. It has many features to process text files and to do system management tasks (as in Perl). It is simple, straight-forward, extensible, and portable ... and free of charge".
On the grounds of above, I decided to give it a try.
Installation
I was surprised to find out that my Mac X Tiger has embedded Ruby interpreter. So users using newest Mac's don't have problems with installation. Despite this, i decided to install it on my Ubuntu Breeze, by running sudo apt-get install ruby. As a result, Ruby was installed on my Linux.
My first script
To get an idea about this programming language, I decided to write a little program, that reads two text files containing mainly numerical data, organized into tables (tabulator as separator between columns). Below is an example of file's content:
As can be seen from the table above, it is typical text file with data. I used two such files, but with different numerical data. I wanted to write some script that takes those to files, performs some mathematical operation on numerical data, and gives summary of calculation.
In this moment I'm not going to describe or explain further details attributed to this script, or give any example code. Just want to share my opinion about my first impression of Ruby.
First steps
I must say, that starting to learn Ruby, was quit easy. This is mainly because of book-like tutorials available at Ruby's homepage. I hadn't have any major difficulties in writing my script. However, there was one thing which caused a little of confusion at the start - iterators like in line (2):
myarray = [1,2,3,4] # (1) create array of name
myarray.each { |i| puts i} # (2) for each element in myarray do 'puts i'-print i
But, after a while, you can get used to them, and what's more important, see how useful they are.
There are many more other inventions in Ruby, which I will be describing as time goes.
Conclusion
In conclusion, I must say my program was written relatively fast. I didn't have any bigger problems with it. However, there was huge problem with making intersection of two arrays of objects, but my own solution to this obstacle, I will describe later. Despite this I must definitively say that I will continue to use Ruby, because it is very clean and easy language.
Mysql, mysqld.sock missing
/usr/bin/mysqladmin: connect to server at 'localhost' failed error: Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock (2)' Check that mysql is running and that the socket: //var/run/mysqld/mysqld.sock' exists !
Normally mysqld.sock is created by running mysqld command. Unfortunately, this command gave:
/usr/sbin/mysqld: Cant't open file: 'host:MYI'. (errno: 142) Fatal error: Can't open privilege tables: File = '/usr/share/mysql/charsets/?.conf' not found (Errcode: 2)
First I tried mysqld --default-character-set=latin1 but it did not help. So I finaly executed
myisamchk -dvv host.MYI
and I saw
myisamchk: Character set '#'83' is not a compiled character set and is not specified in the '/usr/share/mysql/charsets/Index' file myisamchk: error: 142 when opening MyISAM-table 'host.MYI'
The solution was to add to file /usr/share/mysql/charsets/Index only one line:
cp1251 83
Sunday, September 10, 2006
Perl: Creating empty arrays and hashes
The problem was with deleting or emptying and array or hash.
For example, let assume that we have one array @a, and one hash %h:
my @a = ( 2, 3, 4);
my %h = ('me'=>2, 'you'=>3, 'he',=>'4');
print 'Size of @a: ', scalar @a; #gives 3
print 'Size of %h: ', scalar keys %h; #gives 3
Now, we want to delete its content. What my friend did, was:
@a=[]; #WRONG: should be @a=();
%h={}; #WRONG: should be %h=();
print 'Size of @a: ', scalar @a; #gives 1 !!!
print 'Size of %h: ', scalar keys %h; #gives 1 !!!
This little mistake cost him a lot of hours of looking for bug in his code. What he did, he created anonymous empty array [] and anonymous empty hash {} and assign them as first element in $a[0] and as first key in %b.
Hence, the correct code to empty an array or a hash is:
@a=();
%h=();
print 'Size of @a: ', scalar @a; #gives 0 !!!
print 'Size of %h: ', scalar keys %h; #gives 0 !!!
Thursday, August 24, 2006
Matlab and Perl: returning matrixes and cells from Perl to Matlab
In addition to those languages, there is also possibility to use Perl programs within Matlab scripts. This can attributed to the fact, that Matlab, by default, has embedded Perl interpreter (in fact, it's tripped-down version of the interpreter). Therefor, you can use Perl scripts without any problems.
However, in contrast to ANSI C, Java SDK , Fortran interfaces, Matlab doesn't have any build in routines, to pass and return matrices, cells, vectors to and from Perl. The only thing returned to Matlab is string, which represents stdout of Perl script, meaning, that Matlab receives everything what is returned by print functions in Perl. Everything which normally is printed on stdout, is assigned to one string variable in Matlab. For instance if myscriptInPerl.pl is simple perl script print to console something, then executing this in Matlab console:
A = perl('myscriptInPerl.pl', var1,' var2', var3);
assigns to variable A, all output of myscriptInPerl.pl.
Based on that, my own solution to get matrices, cells, vectors, etc. returned from Perl, is to return string, representing Matlab's code, and executing function eval(A)
in Matlab. For example
#myperlscript.pl
#Example file, with Perl scribt to be executed by Patlab
#
$out = "myVector = [2 3 4 5 5 6]; "
#variable with string representing Matlab vector
print $out; #print $out to Matlab
Executing above script in Matlab and evaluating returned string will create
myVector
variable in Matlab:A = perl('myscriptInPerl.pl', var1,' var2', var3);
eval(A);
myVector =
2 3 4 5 5 6
Installation of Kubuntu 6.06
Few days ago I was helpping one friend with installation of Kubuntu 6.06 on a PC. I have to admin that personally I would prefer ubuntu, rather then Kubuntu, but he is a fan of KDE environment, so we downloaded it.
I haven't seen Ubuntu yet, so Kubuntu is my first insight into Dapper Drake (DD) family, which has drawn a lot of public attention lately, as being first distribution from Canonical with three and five year support, for desktop and server usage respectively. Since the release, I have read plenty of reviews and opinions about both Ubuntu and Kubuntu. Majority of them was positive about them, and many authors have stayed that (K)Ubuntu is definitively going in the right direction. Due to the fact there is huge amount of available reviews I'm not intending to write one more. Instead, I just want to describe my own experience and feeling about Kubuntu.
To begin with, I need to write that everything went smoothly, and there was not any single problem with installation, even though this PC was more then seven years old.
First positive aspect of Dapper Drake that I noticed is, that the CD you download, both Live and installation CD. Owning to this I could first check if Kubuntu would be working on my PC. I other world's, I could check if Internet connection, printer, etc. was OK. Afterward, knowing that my hardware worked, I started the installation.
Process of installation went smoothly, within surprisingly short time.
After that, I needed to install some additional stuff to my new operating system, so my first thought was to start Synaptic. And I was quite surprised, to find out that there was no Synaptic (graphical package management program for apt). So, no problem, I just run: sudo apt-get install synaptic and there was another surprise - nothing happened. So still, no problem, I open /etc/apt/sources.list and i had to manually unhashed few repositories. Finally, following all those operation I maneged to install what I wanted. Nevertheless, I had and still have mixed feelings about this.
As most of the users I (my friend) wanted to have mp3 support, video codecs, and other popular things, which desktop user must have, and they are not included in (K)Ubuntu by default, on account of licents difficulties. Because of that, I used easyubuntu. This is Python "script that gives the Ubuntu user the most commonly requested apps, codecs, and tweaks that are not found in the base distribution". It worked perfectly, in few minutes I had everything I wanted, and everything was working (audio/video).
Next, I needed to setup printer. I use netwerk HP printer. And there was no surprise, niether. It just worked, after finding it on the list of all printers in configuration panel.
The last stage, was to install VMware server. Unfortunately, this must be done manually, because there are no apt packages. So, I registered (to download vmware you must to register in order to receive free serial number) and I began the installation. There was another nice surprise, everything went perfectly. All process of vmware setup, consists of accepting default parameters :-). That was it:-)
So in conclusion, I strongly feel that Dapper Drake is good or even very good distribution, if you consider only first impression only. However, that little difficulty with Synaptec and apt, can be problem for newbies. I am sure, that my sister, would have huge problems with that, and what's more, I don't see any reasons why it is done like this.
This was my first text about Kubuntu 6.06. I reckon that during further exploitation there will be something new and maybe more interesting to write about.
Gmail and importing mails from other mail account
Some time ago, I switched from my old webmail, to new gmail provided by google.com.
Everything would be perfect if not the fact that gmail, does not have option for importing e-mails form other pop3 servers.
The only solution I found was to download all you emails using e.g. MS Outlook or Evolution. After this, it is necessary to run special program, which will send all your just downloaded mails to gmail account. It isn't the best solution to this problem, but I could not find enythig else.
Fortunetlly, I have access to Linux server with reasonable bandwich, so I downloaded all my mails from former account (more than 600MB) using Evolution mail client. It is client which goes with Gnome as default. Following this, you need to use one of available programs (gExodus - only Windows, gml, ngml) to export you mails from e.g. Evolution to gmail account. Evolution stores all emails in mbox files. If you use Ubuntu Breeze 5.10, this file shoud be somewhere in you home directory.
To export all you emails stored in mbox you can use e.g. gml python script. However, this script wasn't working for me. So, I found modyfied gml (ngml).
Example usage of ngml:
ngml mbox "/home/me/.mail/Inbox/Inbox.mbox" myaccount@gmail.com
And, finally it worked. All those operation were performed on Ubuntu server, which was used, in this case as SMTP server to send all those emails.
Nevertheless, I found it very strange that you have to do so many tricks to import your mails to gmail account. I wander why it is so complicated?
Monday, July 24, 2006
Gmail and plus-addressing
What are advantages of this plus-addressing? Thanks to this, we can have one email account, and we can have few addresses just by adding tags. Real power of this system can be seen using gmail accounts, when there is possibility to tell what should be done automatically with incoming email containing additional string after '+' sign. In another words, i gmail such action are accessible by filters.
I will show how to create a filter. Lets create filter that looks for tag: 'todo'. Because of that me email address: someone+todo@gmail.com. Such email for example I will give to my boss that he could send me mails to that addres, and in my gmail account they automatically will be assigne to a label called e.g.: 'work_to_do'.
To begin with we need to create 'work_to_do' label. To do this, you go to Setting and than Labels in your gmail account. There new label can be created:
Then filter must be created. To make a filter first click on 'Create a filter':
and after that you create filter in two stages. Frist, you define your tag (string after plus sign in your email address):
In the second step you define what action should be done with such email. In this case we want to assign label 'work_to_do'. To do this, you check 'Apply the label' and select label to apply and you create filter:
And that's it. From this moment on, you can recieve emails in the form of someone+todo@gmail.com, and they automatically are labeled 'work_to_do'.
Of course, you can consequently create others 'plus-addresses', and you can create different actions for them.
Your PHP installation appears to be missing the MySQL which is required for WordPress
Your PHP installation appears to be missing the MySQL which is required for WordPress.
I was quite suprise to see it, couse I hadn't been doing anything with my server configuration before, and yet something happend and this site was not working. What's more, my apache, php, mysql, phpmyadmin were working perfectly.
So, I did a little bit of googling, and I found solution on wordpress officiale site here.
I needed to add those lines just before mysql test in wp-settings.php:
if (!extension_loaded('mysql')){
if (!dl('mysql.so')) {
exit;
}
}
I also tried the second possible solution listed there, but it wasn't working.
Saturday, June 24, 2006
Mounting nfs share
When my nfs server is running, it would be nice to mount this share. I use Ubuntu 5.10 as one mounting the nfs share. In fact, I have access to few Ubuntu based servers, so my experience is mainly base on Ubuntu distribution.
Firts of all, to mount on Ubuntu you need root privilages. Asummig you have them, mounting is childish.
Actual mounting can be made in two ways:
- Manually - as root you mount using mount command:
mount -t nfs 196.17.43.65:/home/me/mynfs /home/menew/mounted_nfs
where:- -t nfs - we mount nfs
- 196.17.43.65:/home/me/mynfs - IP and sheared folder
- /home/menew/mounted_nfs - where this share should be installed on our local file system
- Using /etc/fstab
Add one line to /etc/fstab describing your nfs volume e.g.: 196.17.43.65:/home/mwolski/mynfs /home/menew/mounted_nfs nfs noauto,rw,users,owner 0 0
Thanks to this to mount you as a normal user can execute
mount /home/menew/mounted_nfs
To unmount nfs partition, you invoke umount /home/menew/mounted_nfs just like normal other partition.
Used options in fstab
- noauto - the nfs is mounted only explicitly,insead of being mount at bootup
- users - normal user can mount this volume
- owner - only owner of local directory (/home/menew/mounted_nfs) can mount
- rw - mount is read/write
Friday, May 05, 2006
Ruby: One click Installer
The funny thing about this is, that when you working mostly with Unix based systems (Mac X, Linux, Solaris) many things you assume for granted, like presence of Tk. I forgot that on Windows there is no Tk, just like many other things :-)
Second thing is rather not Ruby but Tk itself. I think that combo boxes are quite useful in GUI applications; nonetheless, Tk does not have combo box widgets. This I already knew from Perl, but with comparison to Ruby, Perl has third party modules that provide combobox functionality eg. JComboBox, Tix. Problem with Ruby is that there is no combobox out of the box, and even though there is Tix port to Ruby (here), its documentation is in Japanese, so for me and for many other programmers it rather not very useful.
Wednesday, April 19, 2006
Latex: Sorting and referencing using bibtex
\usepackage[super,sort&compress,numbers]{natbib}
This results in reference numbers as shown belowinstead of standard way:
Wednesday, April 05, 2006
Ruby: Real number precision
#!/opt/local/bin/ruby
(0..5).each {|x|
puts x/2
}
the result will be:
0
0
1
1
2
2
As everyone can see those results are quite incorrect, cause Ruby assumes that x/2 is division of two integer numbers; hence, the result also must be integer. Such small thing, can be very unseen and hard bug to detect, especially if long, mathematical function is written.
Correct version of the above code is:
#!/opt/local/bin/ruby
(0..5).each {|x|
puts x.to_f/2
}
The result finally is ok:
0.0
0.5
1.0
1.5
2.0
2.5
I had this bug lately and it took me quite a time to find out why my program gives incorrect results.
Friday, March 03, 2006
imagemagick: apply median filter
for f in *.tiff; do convert -median 2 $f ../outDir/$f; echo $f; done
Sunday, February 12, 2006
Matlab code time execution
But more detail insight into this problem is by profile viewer.
This nice tool shows exactly how much time is used by each line, how many times it is executed. Moreover, profiler also gives some suggestions how to improve execution time.
For example, thanks to profiler I was able to reduce time of one image processing program from few hours to few minutes. Profiler showed me, that very simple operation, takes huge amount of time. Just by removing, changing and rewriting few time consuming lines, I was able to significantly shorten exec time of my program.
Thursday, February 09, 2006
Mac X: Changing bash prompt in X11's xterm
PS1="\h:\w \u"
This was very annoying as the prompt contained all the path to the current directory, which was especially bed if I went dip into the directory tree. In such a case it is better to use '\W' option instead of '\w', as this option shows only current directory name, and not full path to it. To change bash prompt use:export PS1="\h:\W \u$ "
To make it permanent just add this line to ~/.bashrc.
Thursday, February 02, 2006
Unreal Tournament 2004: Make it slower
UT2004.ini
file in: \UT2004\System
and change the value of variable GameSpeed
Originally: GameSpeed=1.000000
I use
GameSpeed=0.650000
My computer is not fast enough to render the game in its original speed. Moreover, with slower action, it is easier to observe all those special effects that game has - you can observer, e.g., rocked heading your way, which is pretty nice.Tuesday, January 17, 2006
Latex: draft option
\documentclass[onecolumn,12pt,draft]{article}
Thanks to this embedded pictures are not displayed; instead, only an outline of the box taken up by the picture is shown. This significantly improve rendering.
Sunday, January 15, 2006
Matlab: getargs.m
function S = getargs(defaultS, varglist);
if mod(length(varglist),2) ~=0
error('Odd number of variable parameters');
end
S = defaultS;
i=1;
while i <= length(varglist)
if isfield(S, varglist{i})
S.(varglist{i}) = varglist{i+1};
else
warning_wrap('getargs:unknown_param', ...
['Unknown parameter "' varglist{i} '"']);
end
i = i+2;
end
Tuesday, January 10, 2006
Ubuntu: quick setup of vsftpd server
sudo vim /etc/vsftpd.conf
And uncomment lines:
write_enable=YES
local_enable=YES
and comment line: #anonymous_enable=YES
Finally restart server by:
sudo /etc/init.d/vsftpd restart