Sunday 10 November 2019

Perl Array - Common Functionalities and Usage?

Perl Arrays Functionalities:

We Can perform many functionalities using arrays among them below are some of the most used functionalities in perl

Method 1: [find only Duplicate Elements]

@a=(6,6,4,5,7,9,9);
for (@a) {
    push @dup, $_  if ($h{$_}++ == 1)
}
print @dup;


Method 2:[find largest number from an array] 

@sorted = sort { $a <=> $b } @a;
print @sorted[-1];


Method 3:[push and pop functionalities]

push adds the element at  end of  array

push(@a,"one");
push(@a,"two");

pop removes  last element of  array
pop @a

Method 4:[shift and unshift]

shift removes  first element of  array

shift @a;

unshift adds  element  front of array

unshift(@a,'one');

Method 5:[Finding length of an array elements]

1.
@a=(6,6,4,5,7,9,9);
print $#a+1;

2.
print scalar @a;

3.
$b=@a;
print $b;

Method 6:[Emptying array elements]

$#a=-1;

Method 7:[reversing array elements]

@a=(6,6,4,5,7,9,9);
print reverse @a;


Labels: ,

0 Comments:

Post a Comment

Note: only a member of this blog may post a comment.

<< Home