Variable Type: Scalars & Array(List)

                                                                                                                         

 

Variable Type: Scalar & Array

Scalar :

  • A scalar is a single unit of data.
  • To declare scalar variable in perl, Use $ sign before variable name .

Example:

$ my =4;

$ my =“Sachin Kumar”;

$ my =4.5;

$ my=“a”;

 

List/Array:

  • Lists are groups of scalars and usually reflect multiple values of the same type of data.
  • Array variables are preceded by an “at” (@) sign.
  • There are several ways to represent lists:

Example:

@p=(“India”, 12, 16.65,023);

Creating list variables:

  • List variables are similar to scalar variables except the $ used as the first letter of a scalar variable name is replaced by an @ sign for array variables.
  • The array is the variable that had a list assigned to it.

 

qw operator:

  • When you are adding several scalars to a list and don’t want to bother with quotation marks, you can use the qw operator like this:

Example1:

@a=qw(red green 4 $x);

  • The qw operator has a white-space separated list in parentheses, and it converts everything to literals (with no expansion of variables, so $x is a literal).

Example2:

@days = qw/Mon Tue Wed Thu Fri Sat Sun/;

 

Element accessing form list:

  • You can retrieve list (array) elements by using their element index value.
  • All list elements are numbered, starting from zero, left to right.
  • To access an array element, use the element number in square brackets:

@test=(0,1,2,3,4,5);

print @test[3];

  • This will display the fourth element (because of zero-origin) which will be 3

 

Sequential Array:

  • Perl offers a shortcut for sequential numbers and letters. Rather than typing out each element.

Example:

@var_20 = (10..20);

@var_abc = (a..z);

  • Here double dot (..) is called range operator.
  • @var_20 will contain a list of values from 10 to 20;
  • @var_abc will contain a list of character from a to z;

 

Array size:

  • The size of an array can be determined using scalar context on the array – the returned value will be the number of elements in the array:

Example:

@array = (1,2,3);

print “Size: “,scalar @array,”\n”;

Result: Size:3

  • To get the max index of array:

$max_index = $#array;

print “max_index: “,$max_index,”\n”;

Result: max_index:2

 

Adding and Removing Elements in Array :

  • Perl provides a number of useful functions to add and remove elements in an array.
S. N. Types and Description
1 push @ARRAY
It Pushes the values of the list onto the end of the array.
2 pop @ARRAY
Pops off and returns the last value of the array.
3 shift @ARRAY
Shifts the first value of the array off and returns it, shortening the array by 1 and
moving everything down
4 unshift @ARRAY
add list to the front of the array, and returns the number of elements in the new
array.

 

Example :

#!/usr/bin/perl

@name = (“James”,”Herry”,”Raman”);

print “1. \@name = @name\n”;

push(@name, “Peterson”);

print “2. \@name = @name\n”;

unshift(@name, “Jones”);

print “3. \@name = @name\n”;

pop(@name);

print “4. \@name = @name\n”;

shift(@name);

print “5. \@name = @name\n”;

 

Array slicing:

  • You can also extract a “slice” from an array – that is, you can select more than one item from an array in order to produce another array.

Example:

#!/usr/bin/perl

@days = qw/Mon Tue Wed Thu Fri Sat Sun/;

@weekdays = @days[2,3,4];

@weekend=@days[5..6];

print “@weekdays\n”; print “@weekend\n”;

Result :

Wed Thu Fri

Sat Sun

 

Transformation Scalar to Array:

  • You can convert a scalar to an array using the split function, which requires a pattern to be specified that breaks the scalar into an array based on that pattern.
  • The pattern is surrounded by slashes.

Syntax :

split [ PATTERN [ , EXPR [ , LIMIT ] ] ]

  • This function splits a string into an array of strings, and returns it. If LIMIT is specified, splits into at most that number of fields. If PATTERN is omitted, splits on whitespace. Following is the example:

 

Transformation Scalar to Array:

Example:

  • Split a string into words and put into an array

@array = split( /;/, “ Larry; Curly ;Moe ” );

# creates array @array=(“Larry”, “Curly”, “Moe”);

  • Split into characters

@stooge = split( //, “curly” );

#array @stooge has 5 elements: c, u, r, l, y

 

  • Split into words

@array = split(/\s+/, “this is a test”);

# array has 4 elements : this, is, a, test

 

Transformation Array to Scalar:

  • To convert arrays to scalars, use the join function. The join function expects a string and a list as arguments, and joins the list together using the string, returning a string scalar.

Syntax:

join EXPR, LIST

Example 1:

@array = (“Larry”, “Curly”, “Moe”);

$string = join( “;“, @array);

# string = “Larry ;Curly ;Moe”

Example 2:

@stooge = (“c”, “u”, “r”, “l”, “y”);

$string = join( “”, @stooge );

# string = “curly”

 

Merging Array:

  • Because an array is just a comma-separated sequence of values, you can combine them together as shown below:

Example:1:

#!/usr/bin/perl

@numbers = (1,3,(4,5,6));

print “numbers = @numbers\n”;

Result: 1 3 4 5 6

Example:2:

#!/usr/bin/perl

@odd = (1,3,5);

@even = (2, 4, 6);

@numbers = (@odd, @even);

Result: 1 3 5 2 4 6

 

Accessing Element using loop:

Using for Loop:

To access all the element one by one .

for ($x=1; $x<@list; $x++) {

     print @list[$x];

}

  • The @list in the for condition evaluates to a scalar, the length of the list. Using forever Loop.
  • The foreach loop creates an index variable that is equal to each element in the list, left to right. Here’s the same code as the previous one:

foreach $x (@list) {

    print @list[$x];

}

The variable $x is set to each element in @list in turn, so you don’t have to setup the for loop. The foreach loop starts at index 1, so the 0th element is not counted.

 

Array Sorting:

  • To organize a list in some order, there is the sort function.
  • The syntax of the sort function is:

sort list ;

where list is the name of the list to be sorted.

  • By default an ASCII sort is produced. The original list is not modified.

 

reverse function:

  • The reverse function is used with strings to reverse the characters in the string. It returns a reversed string.

Example:

$str1=“JONES”

$str2=reverse($str1);

print $str2;

Result : “SENOJ”;

 

                                                                                                                         

 

Leave a Reply

Your email address will not be published. Required fields are marked *