Sunday, 29 April 2012

Default Arguments in Javascript

I don't know why javascript do not support default arguments. They should add that. Anyhow there is a another way to perform the same operation. Here is an example
In other languages we pass default arguments like this
int function( var=value,var=value)

We don't have this facility in Javascript so we length of code increases a bit. We can do the same by following method.

function sum(a1,b1)
{
a1=a1?typeof a1=="undefined": default-value;
//meaning
//if the type of variable a1 is undefined that means that the user has not passed any value so we can assign
//our own default value.
typeof operator in javascript returns a string telling the type of variable

for eg:
var abc="Piyush";
alert(typeof abc);
this will return string signifying that the variable is string.

It is very simple.
Happy Coding.

Tuesday, 24 April 2012

Recovering MySQL Database without using MySQL

This post will tell you how to recover your databases without using MySQL or when you are not able to access MySQL. I had to do this when while experimenting I ruined my Ubuntu. I had to recover data from it. So I used Ubuntu Live Cd to excess the ubuntu ext4 drive to backup the data. From live cd we cannot access MySQL but I had to backup my database. So I just copied my database folder from /var/lib/mysql. After installing Ubuntu and MySQL and other softwares all over again I thought of just replacing database folder with the one which I copied but that didn't worked.

Recently I had used dpkg-reconfigure to solve some password problem. So I tried using that and my database was working just fine after that.
Now How to do this in detail :P
You will find folder for each of your database in /var/lib/mysq. You need root permissions to view files of this folder. You can do this by executing the following command in the terminal:

$ sudo nautilus

Then navigating to /var/lib/mysql. In this folder you will find one folder for each of your databases and other mysql files. I copied whole of the folder and kept at some safe place. Now after reinstalling Ubuntu and Lamp-server, I had to get my databases back. So I just made the same database in mysql so that a folder in /var/lib/mysql gets created. I did this because I thought mysql has other places where it saves information such as current databases and all. So by doing this thing, mysql will do all of its stuff and update all of its settings. Now just stop mysql services using:

$ sudo service mysql stop

After that I copied my database folder from backup mysql folder to the current mysql folder that is in /var/lib/mysql
You will be needing super user privileges for this. 
Don't think that the work is finished mysql will not work after this. You can check this by opening it in phpmyadmin or through mysql console.It will give some error. Now you have to reconfigure mysql package to make it work. Execute the following command in the terminal.

$ dpkg-reconfigure mysql-server-5.4

After executing this command it will reconfigure whole of the mysql server and will ask for passwords and all. Enter your password for mysql and you are done. Mysql service will automatically start after this. Just check your database after this using phpmyadmin or mysql console. In my case it was working just fine and hoping the same from your side.

Friday, 23 March 2012

Installing CMU Sphinx in Ubuntu 11.10

It took me 2 installation to install CMU Sphinx successfully. But finally now it is working. Actually I also don't know how to install it but the internet helped me out. So the very first thing that you should do to download latest sphinxbase and pocketsphinx from their website.
http://cmusphinx.sourceforge.net/wiki/download/

After downloading the packages Its time for you to download dependencies. Here is the list of packages that I had to install to make it working. Screenshot of my Ubuntu Software Center.


Install all the packages that I have installed today in the screenshot. After installing all the packages now you can extract sphinxbase compressed file and open the directory and open the sphinxbase directory through the terminal and run the following commands which are normally executed to build a packages manually.
./configure
sudo make
sudo make install
Now unpack the pocketsphinx file and enter extracted directory of pocketsphinx using terminal using the cd command. and the above commands again
./configure
sudo make
sudo make install

After executing these command you can also execute this command to clean up all the mess created.
sudo make clean


NOTE: While executing ./configure focus on the output, It still there is any dependencies left to be installed, the configure script will issue an error something like that this package is missing. Install that package and then try again running the ./configure command.
Best of luck
Happy Coding.!! :)

Edit: List of packages so that you can copy and paste

libdpkg-perl
fakeroot
debhelper
libalgorithm-merge-perl
libgstreamer-plugins-base0.10-dev
libalgorithm-diff-xs-perl
po-debconf
libgstreamer0.10-dev
intltool-debian
gir1.2-gst-plugins-base-0.10
build-essential
dpkg-dev
html2text
patch
libalgorithm-diff-perl
pocketsphinx-hmm-wsj1
libpocketsphinx1
libsphinxbase1
pocketsphinx-utils
pocketsphinx-lm-wsj
gstreamer0.10-pocketsphinx
gir1.2-gstreamer-0.10
libqtgstreamer-0.10-0
libglib2.0-dev
zlib1g-dev
libqtglib-2.0-0
libtool
libltdl-dev
autoconf
automake
autotools-dev
bison
m4