5% assure discount from here only

Showing posts with label reverse array. Show all posts
Showing posts with label reverse array. Show all posts

Monday, January 24, 2011

(3) How to reverse array elements?

Solution :

my @a = ( 1 ,4,2, 9,10 );
my $length = scalar(@a);
my @b;
for(my $i=0;$i<=$length;$i++){ my $j = $a[$length - $i]; push(@b, $j); } print @b;

(1) How to remove duplicate element from an array?

Solution :

There are so many solution of that particular problem, but out of
them i had provided the 2 shortest solution overhere.
(1)
use List::MoreUtils qw(uniq);
my @A = (1,2, 2,4, 3, 4, 5) ;
my @B = uniq @A;
print @B;

(2)

my @A = (1,2, 2,4, 3, 4, 5) ;
my %hash = "";

foreach(@A){
$hash{$_} = $_;
}

my @b = keys %hash;
print @b;