.:: Welcome To My Personal Blog ::.
Showing posts with label Perl. Show all posts
Showing posts with label Perl. Show all posts

Sunday, April 24, 2011

Beginner's Guide To Travel With Perl - Part 9

Sorting:

There is a built-in sort function/subroutine in Perl for sorting array elements. This is done by just writing "sort @name_of_the_array;". But it sorts the elements alphabetically according to their ASCII value. This sort function sorts lexically
Example:
use strict;
main(@ARGV);
sub main
{
    my @arr = ("abc", "abcd", "defg", "def", "ijklm", "ijkl", "nop", "nop", "aaa", "AAA");
    print "before sort : @arr\n";        #before sort : abc abcd defg def ijklm ijkl nop nop aaa AAA
    @arr = sort @arr;
    print "after sort : @arr\n";        #after sort : AAA aaa abc abcd def defg ijkl ijklm nop nop
}

Saturday, March 5, 2011

Beginner's Guide To Travel With Perl - Part 8

Function

So, what will be the next? It’s function. In Perl, functions have another name “Subroutines”. It’s almost easy like other programming languages. Let’s see some examples:

Example1:


use strict;
sub pr { #description of subroutine
print "inside the subroutine\n";
}
print "subroutine example\n";
print "calling it\n";
≺ #calling subroutine, only “pr” also works here
print "The End\n";

Beginner's Guide To Travel With Perl - Part 7

Input/Output from user

Let’s see how to take input from user. It can easily be done by . stands for standard input. It can be abbreviated by using simple <>. By declaring a scalar variable and setting it equal to we set the variable equal to whatever will be typed by our user at the command prompt. But here’s a little problem. When we give input in command prompt and press enter, the variable also takes the newline character with the input. As a result an extra newline is printed while printing the value of the variable. To get rid of this problem we can use chomp() function. This function simply removes any erroneous line breaks and spacing from the end of our string.

Beginner's Guide To Travel With Perl - Part 6

Built-in Operators

Arithmetic Operator :

+ Addition
- Subtraction
* Multiplication
/ Division


Beginner's Guide To Travel With Perl - Part 5

Scope - Something Important

It’s time to say something important now. We can declare any variable in two ways – First is writing variable name with $ sign and assigning a value to it. The second is to write variable name $ sign and adding “my” before it when the variable is used first time.

Beginner's Guide To Travel With Perl - Part 4

Branching

We may have to use condition in some programs. The syntax will be the following:
if ( condition ) {
...
} elsif ( other condition ) {
...
} else {
...
}

Beginner's Guide To Travel With Perl - Part 3

Variables

It’s time to say something about variables now. Perl has three types of variables: scalars, arrays and hashes. Let’s think of them as “things”, “lists” and “dictionaries”. All variable names are a punctuation character, a letter or underscore, and one or more alphanumeric characters or underscores in Perl.

Beginner's Guide To Travel With Perl - Part 2

Strings

Let’s say something about strings. A string is a group of characters between two single quotes or two double quotes. If the characters are bounded with single quotes, the elements are not interpreted. But if they are bounded with double quotes, it means that the contents should be interpreted.

Beginner's Guide To Travel With Perl - Part 1

Something about Perl

Perl is a high-level, general-purpose, interpreted, dynamic programming language. Perl was originally developed by Larry Wall in 1987 as a general-purpose Unix scripting language to make report processing easier. Since then, it has undergone many changes and revisions and become widely popular amongst programmers. Perl borrows features from other programming languages including C, shell scripting (sh), AWK, and sed. The language provides powerful text processing facilities without the arbitrary data length limits of many contemporary Unix tools, facilitating easy manipulation of text files. Perl gained widespread popularity in the late 1990s as a CGI scripting language, in part due to its parsing abilities.

Popular Posts