search instagram arrow-down





If scalar is singular in perl then plural in perl are arrays and lists. A list is an ordered collection of scalars. An array is a variable that contains a list. In perl the two terms are used interchangeably, but to be accurate list is the data and array is the variable.

my @array = ( 2, 3, daya, maya, 6 )
when you use the use strict this syntax may be wrong, as having both number and name.
accessing the arrary variable:
$arrary[3]—> 3rd element
For acessing the whole array element, print @array
There are special array eg.
my @sorted = sort @arrary;
my @backwards = reverse @numbers;

Array Indices
Last index of the Array can be retrieved by $#<array-name>
* eg.  $end = $#rocks;
The array size is dynamic, you don’t have to specify the size of the array. It automatically adjust depending upon your uses.There is no limit on its length as long as there is available memory.

-ve index:

* -1 index is used to access the last element of the array.


use strict;
my @var = (2,3,4,5,"daya","maya","test",6827);
print "@var\n";
print "The 3rd variable is: $var[3]\n";
my $lastindex = $#var;
print "The last element is:$var[$lastindex]\n";
print "the -2 index element: $var[-2]\n";

qw shortcut (Quoted words)
We frequently needs the list of words, the qw shortcut helps us to generate the list without typing a lot of extra quote marks.
* @listvar = (“daya”, “maya”, “test”, “ole”, “linux”);
* @listvar = qw(daya maya test ole linux); separate each of the element with the space bar.

range operator
(1..100) ===1 to 100.

List Assignment:
In much the same way as the scalar values are assigned to variable, list value may be also assigned to variables.
($name, $month, $salary) = qw(hari, feb, 5600);

Its very easy to swap in perl unlike in other language such as C which needs the temporary vaiables.

($var1, $var2) = ($var2, $var1);
it swaps the value of var1 and var2.

* $calar
* @rray
* To retrieve whole array , @array-name
* @array1 = @arrya2 —> copy one array to another.

Pop and Push Operators
* Helps to add and retrieve the array elements without using index.
* pop operator takes the last element of the array and returns it.
* push operator adds an element (or a list of elements) to the end of the array.

@array = 1..10
$var1 = pop(@array); #gets 10
$var2 = pop(@array); #get 9
push(@array,100); #push 100 to the end of the array.
push(@array, @others);

The shift and unshift Operators
It works in the same way as the push and pop but unlike push and pop does the operation at the end of the array or list it does at the beginning.
@array = 1..10
$var1 = shift@array; #gets 1
$var2 = shift@array; #gets 2
unshift(@array,5); # insert 5 at the beginning of the array

foreach control structure
The foreach loop steps through a list of values, executing one iteration for each value. foreach statement can be used in two ways.
1. using another variable to retrieve the element.
foreach $i (@array) {
print “$i”;
2.Using Perl default variable $_ to retrieve element.
foreach (@array)
print “$_\n”;
Special Array Operator
* sort operator
@sort = sort(@array); #sorts only the string.
* reverse operator
@rev = reverse(@array);
”’scalar and list context”’
The most important things to understand. The context refers to where an expression is found. As Perl is parsing your expressions,it always expects either a scalar value or a list value.* What Perl expects is called the context of the expression.
42 + something # The something must be a scalar
sort something # The something must be a list

@people = qw( fred barney betty );
@sorted = sort @people; # list context: barney, betty, fred
$number = 42 + @people; # scalar context: 42 + 3 gives 45

Even ordinary assignment (to a scalar or a list) causes different contexts:
@list = @people; # a list of three people
$n = @people;    # the number 3

Forcing Scalar Context
On occasion, you may need to force scalar context where Perl is expecting a list. In that
case, you can use the fake function scalar. It’s not a true function because it just tells
Perl to provide a scalar context:
@rocks = qw( talc quartz jade obsidian );
print “How many rocks do you have?\n”;
print “I have “, @rocks, ” rocks!\n”;        # WRONG, prints names of rocks
print “I have “, scalar @rocks, ” rocks!\n”; # Correct, gives a number
Oddly enough, there’s no corresponding function to force list context.

Using List-Producing Expressions in Scalar Context

$fred = something;            # scalar context
@pebbles = something;         # list context
($wilma, $betty) = something; # list context
”($dino) = something;          # still list context!”[[Link title]]
Don’t be fooled by the one-element list; that last one is a list context, not a scalar one.The parentheses are significant here, making the fourth of those different than the first.

”'<STDIN> in List Context”’

@lines = <STDIN>; # read standard input in list context
press CTRL-D at the last for the end of the input.

@lines = <STDIN>; # Read all the lines
chomp(@lines);    # discard all the newline characters
But the more common way to write that is with code similar to what we used earlier:
chomp(@lines = <STDIN>); # Read the lines, not the newlines

@days{‘a’,’c’}      # same as ($days{‘a’},$days{‘c’})

This entry was posted in Perl.
Leave a Reply
Your email address will not be published. Required fields are marked *

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: