What Is Perl?

Perl is an acronym, short for Practical Extraction and Report Language. It was designed by Larry Wall as a tool for writing programs in the UNIX environment and is continually being updated and maintained by him. For its many fans,

  • Perl provides the best of several worlds. For instance: Perl has the power and flexibility of a high-level programming language such as C. In fact, as you will see, many of the features of the language are borrowed from C.
  • Like shell script languages, Perl does not require a special compiler and linker to q turn the programs you write into working code. Instead, all you have to do is write the program and tell Perl to run it. This means that Perl is ideal for producing quick solutions to small programming problems, or for creating prototypes to test potential solutions to larger problems.
  • Perl provides all the features of the script languages sed and awk, plus features not found in either of these two languages. Perl also supports a sed-to-Perl translator and an awk-to-Perl translator.

In short, Perl is as powerful as C but as convenient as awk, sed, and shell scripts.

How to install perl on Ubuntu

Perl is located in the ubuntu repositories, you can install it by the following command.

sudo apt-get install perl

perl is installed in usr/bin/
Writing your first perl program

#!/usr/bin/perl
# A simple perl program to print the user input
print "Hello, type in something\n";
$inputline= <stdin>;
print ($inputline);

Lets split up the code and see what each line does..
#!/usr/bin/perl
# Says this line is a comment and there are no executable instructions on this line
! Says this is a perl script
/usr/bin/perl give the location of the perl interpretor, many programing books mention the location as usr/loacl/bin/perl, but this is not correct in ubuntu. In ubuntu perl interpretor is located at usr/bin/perl.
print “Hello, type in something\n”;
This line just prompts to user to type something, similar to printf statment in C.
$inputline=<stdin>
Here we are setting a variable named inputline, and storing the input from the keyboard into that variable. <stdin> takes the input from the keybard.
print ($inputline);
This line echoes the typed in message.

Running your first perl program

Copy the above above program in to your favorite text editor and save it as myFirstPerlProgram.pl and then change it to a executable, you can do that by typing the follwing in the terminal

chomod +x myFirstPerlProgram.pl

now you can run this code by typing myFirstPerlProgram.pl in the terminal

Output of the above perl program

Output of the above perl program

Learn javaScript

Learn javaScript

I recently started watching Essentials of javaScript by DoriSmith @ Lynda.Com, It’s very interesting, easy to understand and helpful like many other video tutorials at Lynda.Com.

Video tutorials are very easy to follow, productive and easy to grasp. The only problem I find with video tutorials is, when you get stuck or have some doubt few days after watching the tutorial, it’s hard to refer back. So, its always good to prepare some notes of important points which you can refer back easily. I’m going to share my notes here as I go along, I hope you find it use full.

» Read the rest of the entry..

Stacks are linear data structures. This means that their contexts are stored in what looks like a line (although vertically). This linear property, however, is not sufficient to discriminate a stack from other linear data structures. For example, an array is a sort of linear data structure in which you can access any element directly. In contrast, in a stack, you can only access the element at its top. In a stack Last thing In is the First thing Out. Thus, we say that a stack enforces LIFO order.

One disadvantage of implementing stack using an array is wastage of space. Most of the times most of the array is left unused. A better way to implement stack is by using a linked list. By using a single linked list to implement a stack there is no wastage of space.

Implementation of stack using a linked list

Implementation of stack using a linked list

/*
A simple implementation of linked list in C plus plus (CPP/C++)
*/
/*
Compiler used: g++
*/
#include<iostream>
using namespace std;
struct node{
	int data;
	node *next;
	};
//This function pushes data(node) to the stack
node * push(node*,node*);
//This function pops data (node) and returns pointer to the poped node
node * pop(node*);
//This function shows the stack
void show_stack(node*);
///These two global variables will help us track the start and end of the list
node *f,*l;
main()
{
	int i,ch;
	f=l=NULL;
	cout<<"\t 1 to push"<<endl;
	cout<<"\t 2 to pop"<<endl;
	cout<<"\t 3 to show stack"<<endl;
	cout<<"\t 0 to exit"<<endl;
	while(1)
	{
		cout<<"Enter your choice:";
		cin>>ch;
		switch(ch)
		{
		case 1:
			{
			if(f==NULL)
			l=f=push(f,l);
			else
			l=push(f,l);
			break;
			}
		case 2:
			{
			cout<<"popped:"<<pop(f)->data<<endl;
			break;
			}

		case 3:
			{
			show_stack(f);
			break;
			}
		case 0:
			break;
		default:
			cout<<"Please enter a proper choice"<<endl;break;
		}
		if(ch==0)
		break;
	}
}
node * push(node *f,node *l)
{
	node *n;
	n=new node;
	cout<<"Enter data:";
	cin>>n->data;
	n->next=NULL;
	if(f==NULL)
	{f=l=n;return f;}
	else
	{
		l->next=n;
		l=n;return l;
	}

}
void show_stack(node *f)
{
	cout<<"showing data"<<endl;
	node *guest=f;
	while(guest!=NULL)
	{
		cout<<"\t"<<guest->data<<endl;
		guest=guest->next;
	}
}
node * pop(node *f)
{
	node *guest,*lb;
	guest=lb=f;
	while(guest->next!=NULL)
	{
		lb=guest;
		guest=guest->next;
	}
	lb->next=NULL;
	l=lb;
	return guest;
}
		

Download the program: Implementation of stack using a single linked list

The simplest kind of linked list is a singly-linked list , which has one link per node. This link points to the next node in the list, or to a null value or empty list if it is the final node.A singly linked list’s node is divided into two parts. The first part holds or points to information about the node, and second part holds the address of next node. A singly linked list travels one way.

A singly-linked list containing two values: the value of the current node and a link to the next node

A singly-linked list containing two values: the value of the current node and a link to the next node

/*

A simple implementation of linked list in C plus plus (CPP/C++)

*/
/*
Program downloaded from www.GeeksPlanet.net
For any help, post comment here
http://geeksplanet.net/2008/11/data-structures/implementation-of-singly-linked-list-using-c-plus-plus/
Compiler used: g++
*/
#include<iostream>
using namespace std;
struct node{
	int data;
	node *next;
	};
/*
This function adds a node at the end of the
list and returns pointer to the added node,
This takes pointer to the first node and
pointer to the last node as argument. By
giving the last node as argument we are saving
some computer labour as it need not travel
from the start to the end to find the last node
*/
node * addnode(node*,node*);
/*
This function just takes the first node as
the argument and traverses the entire list
displaying the data in each node.
*/
void shownodes(node*);
/*
This function takes the first node as argument
and traverses the list until it finds the last
node and deletes it and returns pointer to the
new last node.
*/
node * delete_node(node*);
/*
*f and *l are the global variables keeping track
of first and last nodes.
*/
node *f,*l;

main()
{
	int i,ch;
	f=l=NULL;
	cout<<"\t 1 to add a node"<<endl;
	cout<<"\t 2 to see the nodes"<<endl;
	cout<<"\t 3 to delete a node"<<endl;
	cout<<"\t 0 to exit"<<endl;
	while(1)
	{
		cout<<"Enter your choice:";
		cin>>ch;
		switch(ch)
		{
		case 1:
		{
			if(f==NULL)
			l=f=addnode(f,l); //Since first node is the last node
			else
			l=addnode(f,l);	  //Last node is the new node added
			break;
		}
		case 2:
			shownodes(f);break;
		case 3:
			l=delete_node(f);break; // Last node has been changed to the last but one.
		case 0:
			break;
		default:
			cout<<"Please enter a proper choice"<<endl;break;
		}
		if(ch==0)
		break;
	}
}
node * addnode(node *f,node *l)
{
	node *n;
	n=new node;
	//Allocating memory for the new node
	cout<<"Enter data:";
	cin>>n->data;
        //This is going to be the last node, so its next point to NULL
	n->next=NULL;
	//If there is no first node, then the new node is the first and last node.
	if(f==NULL)
	{f=l=n;return f;}
	else
	{
		// Pointing the last node to the new node
		l->next=n;
		//Setting the new node as the last node
		l=n;return l;
	}

}
void shownodes(node *f)
{
	cout<<"showing data"<<endl;
	node *guest=f;
	while(guest!=NULL)
	{
		cout<<"\t"<<guest->data<<endl;
		guest=guest->next;
	}
}
node * delete_node(node *f)
{
	node *guest,*lb;
	guest=lb=f;
	while(guest->next!=NULL)
	{
		lb=guest;
		guest=guest->next;
	}
	lb->next=NULL;
	cout<<"node deleted"<<endl;
	return lb;
}

Download the prgoram linked-list.cpp

A C++ program to generate a Pascal’s triangle which is as follows:

      1
     1 1
    1 2 1
   1 3 3 1
  1 4 6 4 1
<pre>#include<iostream>
using namespace std;
int fact(int);
main()
{
	int rows,i,j,k;
	cout<<"Enter the numbe of rows you want in the triangle:";
	cin>>rows;
	for(i=0;i<rows;i++)
	{
		//Moving each row by rows-i spaces to get a triangular shape
		for(k=0;k<(rows-i);k++)
		cout<<" ";
		//Loop for printing each row
		for(j=0;j<=i;j++)
		cout<<" "<<fact(i)/(fact(j)*fact(i-j)); //nCr=n!/(r!*(n-r)!)
		cout<<endl;
	}
}

int fact(int i)
{
	int value=1;
	while(i!=0)
	{
		value=value*i;
		i--;
	}
	return value;
}</pre>

Download the program Pascals-Triangle

This program is a implementation of shunting yard algorithm to convert an infix expression to post fix expression, This is a extension of the stack program published earlier

Algorithm:(Taken from wikipedia)

  • While there are tokens to be read:
  • Read a token .
  • If the token is a number, then add it to the output queue.
  • If the token is a function token, then push it onto the stack.
  • If the token is a function argument separator (e.g., a comma):
  • Until the topmost element of the stack is a left parenthesis, pop the element from the stack and push it onto the output queue. If no left parentheses are encountered, either the separator was misplaced or parentheses were mismatched.
  • If the token is an operator, o1, then:
  • while there is an operator, o2, at the top of the stack, and either
o1 is associative or left-associative and its precedence is less than (lower precedence) or equal to that of o2, or
o1 is right-associative and its precedence is less than (lower precedence) that of o2,
pop o2 off the stack, onto the output queue;
  • push o1 onto the stack.
  • If the token is a left parenthesis, then push it onto the stack.
  • If the token is a right parenthesis:
  • Until the token at the top of the stack is a left parenthesis, pop operators off the stack onto the output queue.
  • Pop the left parenthesis from the stack, but not onto the output queue.
  • If the token at the top of the stack is a function token, pop it and onto the output queue.
  • If the stack runs out without finding a left parenthesis, then there are mismatched parentheses.
  • When there are no more tokens to read:
  • While there are still operator tokens in the stack:
  • If the operator token on the top of the stack is a parenthesis, then there are mismatched parenthesis.
  • Pop the operator onto the output queue.
  • Exit.
#include <stdio.h>
#define size 10
char stack[size];
int tos=0,ele;
void push();
char pop();
void show();
int isempty();
int isfull();
char infix[30],output[30];
int prec(char);
int main()
{
                int i=0,j=0,k=0,length;
                char temp;
                printf("\nEnter an infix expression:");
                scanf("%s",infix);
                printf("\nThe infix expresson is %s",infix);
                length=strlen(infix);
                for(i=0;i<= prec(stack[tos-1])  )
						{
						temp=pop();
                                                printf("\n the poped element is :%c",temp);
                                                output[j++]=temp;
						push(infix[i]);
						printf("\n The pushed element is :%c",infix[i]);
                                                show();
						}
						else
						{
						push(infix[i]);
						printf("\nThe pushed element is:%c",infix[i]);
                                                show();
						}
					}
					else
					{
						if(infix[i]=='(')
						{
                                                push(infix[i]);
                                                printf("\nThe pushed-- element is:%c",infix[i]);
                                                }
						if(infix[i]==')')
						{
						temp=pop();
                                                while(temp!='(')
                                               {output[j++]=temp;
                                                printf("\nThe element added to Q is:%c",temp);
                                                //temp=pop();
						printf("\n the poped element is :%c",temp);
						temp=pop();}
						}
					}
				}
			}
 printf("\nthe infix expression is: %s",output);
		}
		while(tos!=0)
		{
			output[j++]=pop();
		}
printf("the infix expression is: %s\n",output);
}
//Functions for operations on stack
void push(int ele)
{
	stack[tos]=ele;
	tos++;
}
char pop()
{
	tos--;
	return(stack[tos]);
}
void show()
{
	int x=tos;
	printf("--The Stack elements are.....");
	while(x!=0)
	printf("%c, ",stack[--x]);
}

//Function to get the precedence of an operator
int prec(char symbol)
{
if(symbol== '(')
return 0;
if(symbol== ')')
return 0;
if(symbol=='+' || symbol=='-')
return 1;
if(symbol=='*' || symbol=='/')
return 2;
if(symbol=='^')
return 3;
return 0;
}

Download the Program Infix-to-postfix.c

/////////////////////////////////////////////////////

// Implementation of stack(containing names) as an //

// array, with its basic operations PUSH,POP and   //

// also the IS_EMPTY and IS_FULL operations.       //

// Program written by Satish Gandham on 01-09-08   //

// Feel free to modify or use it directly          //

// Program compiled with GCC Compiler              //

/////////////////////////////////////////////////////

#include<stdio.h>
#define size 5
char stack[size][15],ele[20];
int tos;
void push();
char* pop();
void show();
int isempty();
int isfull();

int main()
{
	int choice;
	tos=0;
	do
	{
		printf("\tEnter 1 for push,2 for pop,3 to show,and 4 to exit\n");
		scanf("%d",&choice);
		switch(choice)
		{
		case 1:
		if (isfull())
		printf("\nThe stack is full");
		else
			{
		printf("\n Enter element to insert");
		scanf("%s",ele);
		push(ele);
			}
		break;
		case 2:
		if(isempty())
		printf("\n The stack is empty");
		else
		printf("\nThe removed element is:%s",pop());
		break;
		case 3:
		if(isempty())
		printf("\nThe stack is empty, I cant show you any elements");
		else
		show();
		break;
		case 4:
		exit(1);
		default:
		printf("\nDo you understand english and numbers??");
		}
	}while(1);
}
int isempty()
{
	return(tos==0);
}
int isfull()
{
	return(tos==size);
}
void push(ele)
{
	//stack[tos]=ele;
	strcpy(stack[tos],ele);
	tos++;
}
char* pop()
{
	tos--;
	return(stack[tos]);
}
void show()
{
	int x=tos;
	printf("\nThe Stack elements are.....\n");
	while(x!=0)
	printf("\t%s\n",stack[--x]);
}

Download the Program

I was trying to write a simple program in c and it kept on showing the following error whenever i tried to compile it

“error: stdio.h: No such file or directory”

After a little googling I found that C standard libraries are stored in libc6-dev, This want installed in my machine. Installing libc6-dev solved the problem.

To intall libc6-dev In ubnutu type in the follwoing in the terminal

sudo apt-get install libc6-dev