Conditional and Loops:
Let me explain looping constructs other than Swith-case.
if-elsif-else
Following is the syntax for if-elsif-else .
if(condition)
{
......;
}
elsif(condition)
{
.........;
}
else
{
..........;
}
Following is the contradicted version of it
unless(condition)
{
........;
}
if the condition returns false, then the set the statements in between braces gets executed.
This is similar to if(!condition)
Braces after the if statement are required if there are bunch/block of statements to execute.
Following example explains in detail...
my $var=1;
if($var==1)
{
print "Bunch of Statements"."\n";
}
$var=0;
unless(!$var==1)
print("I am in unless condition");
Output:
Bunch of Statements
I am in unless condition
While statement
Following is the syntax for While conditional statement
while (condition)
{
........;
}
Following is the contradicted version of it
untill(condition)
{
...........;
}
Following is the example for while conditional statement
my $num=1;
while($num)
{
print "I am in While"."\n";
$num=0;
}
until ($num==1)
{
print "I am in untill"."\n";
$num=1;
}
Output:
I am in While
I am in untill
For Loop
for(variable_initialization;condition;iteration)
{
........;
}
Following is the example for 'for' loop
my $num=3;
my $vi;
for($vi=0;$vi<=$num;$vi++)
{
print $vi."\n";
}
Output
0
1
2
3
Foreach statement
Following is the syntax for 'foreach' statement
foreach(condition)
{
.............;
}
Example
my @ar=("One","Two","Three");
foreach(@ar)
{
print "List element $_"."\n";
}
Output:
One
Two
Three
Tuesday, September 8, 2009
After such a long time I am posting few of my learning's on Perl...
Variable Scoping:
In general variables are declared using "my".
my $var= "hello";
Actually its not required. Just use
$var="hello";
Both the statements print "hello".
But the second statement creates global variables throughout the program, which is not recommended.
Using 'my' creates lexically scoped variables. Say bunch of statements surrounded by curly-braces.
The variables declared in between braces are different.
my $var="Hello";
my $num=1;
if($num==1)
{
my $var1="World";
print $var."\n"; #prints Hello
print $var1."\n"; #prints World
}
print $var1; #prints nothing
Output:
Hello
World
Using "my" in combination with a 'use strict;' at the top of the Perl script means that the interpreter will pick up certain common programming errors.
Add 'use strict;' at the top of the script and execute the same script. The final print statement 'print $var1;' would display compile-time error and fail to execute the script.
So add 'use strict;' in the program, which is recommended.
Variable Scoping:
In general variables are declared using "my".
my $var= "hello";
Actually its not required. Just use
$var="hello";
Both the statements print "hello".
But the second statement creates global variables throughout the program, which is not recommended.
Using 'my' creates lexically scoped variables. Say bunch of statements surrounded by curly-braces.
The variables declared in between braces are different.
my $var="Hello";
my $num=1;
if($num==1)
{
my $var1="World";
print $var."\n"; #prints Hello
print $var1."\n"; #prints World
}
print $var1; #prints nothing
Output:
Hello
World
Using "my" in combination with a 'use strict;' at the top of the Perl script means that the interpreter will pick up certain common programming errors.
Add 'use strict;' at the top of the script and execute the same script. The final print statement 'print $var1;' would display compile-time error and fail to execute the script.
So add 'use strict;' in the program, which is recommended.
Tuesday, March 3, 2009
Anonymous Variables @ PERL
My friend had told me how anonymous variables are handled in PERL. I really felt very interesting... let me share the same with you...
Anonymous Variables: If a variable is declared in anonymous mode, it treats like memory allocation. It's important first to understand how your OS and libraries handle memory and depends on 32-bit/64-bit machine.
These variables are not referred by names.
Let's look into the details how PERL handles these variables...
Scalar Variable Handling Array values...
A reference is a scalar value and can refer to any other Perl data type.
A scalar variable can be declared providing list of array values.
>my $Scal = [1, 2, 3, 4];
Here the scalar variable wont handle the list of values, it handles the address where the array is located.
Each and every variable in the list is accessed using its index.
For Example:
#Annonymous for ARRAY
my $var1 = [1,2,3];
print("Array details are ");
foreach (@{$var1})
{
print ;
}
Result:
Array details are 123
Note: @{var1} is an array, but when it is used in scalar context then it returns number of elements contained in it.
Scalar Variable Handling Hash Variables...
Storing a reference as the value of an hash element, can easily create lists and hashes within lists and hashes.
For Example:
#Annonymous for HASH
my $var1 = { aa => "Synergy", ab => "Change", ac => "Integrations" };
foreach ( keys(%$var1) ) #returns the Key values for Hash Variable
{
print "$$var1{$_} \n";
}
Result:
Change
Integrations
Synergy
Here '$_' refers to "default variable",It's used as the default argument to a number of functions in Per
For Example:
#Annonymous for HASH
my $var1 = { aa => "Synergy", ab => "Change", ac => "Integrations" };
print "\nFirst Value =>" ,"$var1->{'aa'}" . "\n";
Result:
First Value =>Synergy
Here it goes another interesting example...
my $var1 = { aa => { a => 'Change', b => 'Integration'} };
print "$var1->{'aa'}->{'a'}\n";
Result:
Change
This is 2 level hash of hash structure using anonymous hash references. Sounds Cool!!!
Anonymous Variables: If a variable is declared in anonymous mode, it treats like memory allocation. It's important first to understand how your OS and libraries handle memory and depends on 32-bit/64-bit machine.
These variables are not referred by names.
Let's look into the details how PERL handles these variables...
Scalar Variable Handling Array values...
A reference is a scalar value and can refer to any other Perl data type.
A scalar variable can be declared providing list of array values.
>my $Scal = [1, 2, 3, 4];
Here the scalar variable wont handle the list of values, it handles the address where the array is located.
Each and every variable in the list is accessed using its index.
For Example:
#Annonymous for ARRAY
my $var1 = [1,2,3];
print("Array details are ");
foreach (@{$var1})
{
print ;
}
Result:
Array details are 123
Note: @{var1} is an array, but when it is used in scalar context then it returns number of elements contained in it.
Scalar Variable Handling Hash Variables...
Storing a reference as the value of an hash element, can easily create lists and hashes within lists and hashes.
For Example:
#Annonymous for HASH
my $var1 = { aa => "Synergy", ab => "Change", ac => "Integrations" };
foreach ( keys(%$var1) ) #returns the Key values for Hash Variable
{
print "$$var1{$_} \n";
}
Result:
Change
Integrations
Synergy
Here '$_' refers to "default variable",It's used as the default argument to a number of functions in Per
For obtaining individual values we need to refer to each elementFor Example:
#Annonymous for HASH
my $var1 = { aa => "Synergy", ab => "Change", ac => "Integrations" };
print "\nFirst Value =>" ,"$var1->{'aa'}" . "\n";
Result:
First Value =>Synergy
Here it goes another interesting example...
my $var1 = { aa => { a => 'Change', b => 'Integration'} };
print "$var1->{'aa'}->{'a'}\n";
Result:
Change
This is 2 level hash of hash structure using anonymous hash references. Sounds Cool!!!
Wednesday, February 25, 2009
PERL Variables... Learning 1
Today I told my friend about my interests in gathering the details…
Lucky the person is ready to share the most possible information with us…
Let me share... the learning's of the day...
PERL VARIABLES:
PERL variables are classified into 3 types. They are as follows...
1. Scalar
2. Array
3. Hash
Scalar Variables:
Scalar Variables are declared using ‘$’. The same variable is capable of handling both strings and numbers values.
For example:
my $sca = 9;
print $sca;
print “\n”;
my $sca = “Scalar Variables”;
print $sca;
The output is:
9
Scalar Variables
Array Variables:
Array variables are declared using ‘@’. We can say list of scalars can be an array variable. Similarly like scalar, array’s also handles both numbers and strings.
For example:
my @arr = (“a”,”b”,”c”);
print 'First value ' . $arr[0];
print 'Second value ' . $arr[2];
The output is:
First Value a
Second Value b
Array variable are classified as variable, its index and value. In the above example ‘arr’ is array, ‘[0]’ is index and ‘a’ is the value.
Indices of an array variable starts with ‘0’ (Zero).
‘$’ can be used as scalar value of an array ‘@’.
To know the number of elements in the array, use '#' before the variable name and it return the index count.
>my @Scal = ("a","b","c","d");
> print "SIZE OF ARRAY $#Scal";
Output:
SIZE OF 3
Hash Variables:
Hash variables are declared using ‘%’. It’s the variant of the array. In array it uses indexing, but hash uses keys for indexing. So hash is classified as key and its value.
For example:
my %has = (‘a’ => ‘1’,’b’ => ‘2’,’c’ =>’3’);
print $has(‘a’);
The output is:
1
These are not arranged in particular order or the way the keys are declared. The values can only be accessed by its key rather than anything else. The values in the Hash variable can be handled by its key; the each key value is again a scalar value.
Hash Variables are shorter than "associative arrays".
Lucky the person is ready to share the most possible information with us…
Let me share... the learning's of the day...
PERL VARIABLES:
PERL variables are classified into 3 types. They are as follows...
1. Scalar
2. Array
3. Hash
Scalar Variables:
Scalar Variables are declared using ‘$’. The same variable is capable of handling both strings and numbers values.
For example:
my $sca = 9;
print $sca;
print “\n”;
my $sca = “Scalar Variables”;
print $sca;
The output is:
9
Scalar Variables
Array Variables:
Array variables are declared using ‘@’. We can say list of scalars can be an array variable. Similarly like scalar, array’s also handles both numbers and strings.
For example:
my @arr = (“a”,”b”,”c”);
print 'First value ' . $arr[0];
print 'Second value ' . $arr[2];
The output is:
First Value a
Second Value b
Array variable are classified as variable, its index and value. In the above example ‘arr’ is array, ‘[0]’ is index and ‘a’ is the value.
Indices of an array variable starts with ‘0’ (Zero).
‘$’ can be used as scalar value of an array ‘@’.
To know the number of elements in the array, use '#' before the variable name and it return the index count.
>my @Scal = ("a","b","c","d");
> print "SIZE OF ARRAY $#Scal";
Output:
SIZE OF 3
Hash Variables:
Hash variables are declared using ‘%’. It’s the variant of the array. In array it uses indexing, but hash uses keys for indexing. So hash is classified as key and its value.
For example:
my %has = (‘a’ => ‘1’,’b’ => ‘2’,’c’ =>’3’);
print $has(‘a’);
The output is:
1
These are not arranged in particular order or the way the keys are declared. The values can only be accessed by its key rather than anything else. The values in the Hash variable can be handled by its key; the each key value is again a scalar value.
Hash Variables are shorter than "associative arrays".
PERL Docs
You know why I started with PERL...PERL is an intriguing language.
I admire PERL scripting a lot...esteem people who do scripting and who like learning it...
I am not a Hacker...just a learner :)
Giving the PERL Org. URL and installer path won't fulfill my exertion...
In general when we say about some product... people tend to ask...
"Do you have any documents for that...?"
Before you ask me...let me post the links for PERL document reference...
Here it goes...
http://perldoc.perl.org/
Friend of mine installed Strawberry PERL, she was unable to run the scripts... here are the additional things we need to do after installing PERL
Set the 'PATH' environmental variable to the location where you installed Strawberry PERL; say 'C:\Strawberry\bin\'.
Open Edit Plus or Notepad ++ and trying the example mentioned below
This the sample example...
Save the file in certain location, with extension '.pl'.
Open the command prompt, navigate to the location where PERL scripts are saved. Run the following command.
>perl FILENAME.pl
Note: FILENAME.pl is the name of the file you saved earlier.
Have a Happy Start :)
I admire PERL scripting a lot...esteem people who do scripting and who like learning it...
I am not a Hacker...just a learner :)
Giving the PERL Org. URL and installer path won't fulfill my exertion...
In general when we say about some product... people tend to ask...
"Do you have any documents for that...?"
Before you ask me...let me post the links for PERL document reference...
Here it goes...
http://perldoc.perl.org/
Friend of mine installed Strawberry PERL, she was unable to run the scripts... here are the additional things we need to do after installing PERL
Set the 'PATH' environmental variable to the location where you installed Strawberry PERL; say 'C:\Strawberry\bin\'.
Open Edit Plus or Notepad ++ and trying the example mentioned below
This the sample example...
#! /usr/bin/perl
use strict;
use warnings;
my $greeting = "Hello, World!";
print $greeting,"\n"; # Say hello
Save the file in certain location, with extension '.pl'.
Open the command prompt, navigate to the location where PERL scripts are saved. Run the following command.
>perl FILENAME.pl
Note:
Have a Happy Start :)
Give a try to PERL Scripting...
For people who are starting with PERL, the following link helps alot...
http://www.perl.com/pub/a/2008/05/07/beginners-introduction-to-perl-510-part-2.html
Inorder to work on PERL, get the installer from the following location.
http://strawberryperl.com/
Just install PERL and start creating your own PERL files...
Start with print statements... this is the step we initially follows for all programming languages...
http://www.perl.com/pub/a/2008/05/07/beginners-introduction-to-perl-510-part-2.html
Inorder to work on PERL, get the installer from the following location.
http://strawberryperl.com/
Just install PERL and start creating your own PERL files...
Start with print statements... this is the step we initially follows for all programming languages...
Subscribe to:
Posts (Atom)