@ascending = sort {$a cmp $b} @a;
@descending = sort {$b cmp $a} @a;
(OR)
@descending = reverse @ascending;
Wednesday, July 21, 2010
appending & reversing arrrays
##Appending arrays
@c = (@a, @b);
##Reversing an array
@arr = reverse (@arr);
@c = (@a, @b);
##Reversing an array
@arr = reverse (@arr);
splice function
It is used to inseret the elements of one array into the middle of another array.
Computing Union, intersection, or Diffrences of unique lists...
@a = (1, 3, 6, 7, 8);
@b = (2, 3, 5, 7, 9);
@union = @isect = ( );
%union = %isect = ( );
foreach $e (@a) {
$union{$e} = 1;
}
foreach $e2 (@b) {
if ($union{$e2} ) {
$isect {$e2} = 1;
}
$union{$e2} = 1;
}
@union = keys %union;
@isect = keys %isect;
@b = (2, 3, 5, 7, 9);
@union = @isect = ( );
%union = %isect = ( );
foreach $e (@a) {
$union{$e} = 1;
}
foreach $e2 (@b) {
if ($union{$e2} ) {
$isect {$e2} = 1;
}
$union{$e2} = 1;
}
@union = keys %union;
@isect = keys %isect;
Finding elements in one array but not other...
@A = qw(a, b, c, d, e);
@B = qw(a, b, e);
@Aonly= ();
%seen = ();
foreach $ele (@B) {
%seen{$ele} = 1;
}
foreach $ele (@A) {
unless ($seen{$ele)) {
###push that element if not seen already.
push (@Aonly, $ele);
}
}
######2nd method##
@seen{@B} = ();
foreach $ele (@A) {
### push that element if it doesn't exist in @B or not seen in @B
push (@Aonly, $lele) unless exists $seen{$ele} ;
}
@B = qw(a, b, e);
@Aonly= ();
%seen = ();
foreach $ele (@B) {
%seen{$ele} = 1;
}
foreach $ele (@A) {
unless ($seen{$ele)) {
###push that element if not seen already.
push (@Aonly, $ele);
}
}
######2nd method##
@seen{@B} = ();
foreach $ele (@A) {
### push that element if it doesn't exist in @B or not seen in @B
push (@Aonly, $lele) unless exists $seen{$ele} ;
}
Extracting unique elements from a list...
@list = qw(a, b, c, d, a, e, b);
%seen = ( );
@uniq = ( );
foreach $item (@list) {
unless ( $seen{$item} ) {
#### we have not seen this item before
$seen{$item} = 1;
push(@uniq, $item);
}
}
##########faster method ####
foreach $item (@list) {
push (@uniq , $item) unless $seen{$item}++;
}
######simple method####
@uniq = sort -u @list;
%seen = ( );
@uniq = ( );
foreach $item (@list) {
unless ( $seen{$item} ) {
#### we have not seen this item before
$seen{$item} = 1;
push(@uniq, $item);
}
}
##########faster method ####
foreach $item (@list) {
push (@uniq , $item) unless $seen{$item}++;
}
######simple method####
@uniq = sort -u @list;
Reference to arrays
Aim: Accessing elements from reference to array. Define an array, put reference to that and access elements from that reference,
@arr = qw(apple orange banana);
##reference to this array is ==> ref_to_arr scalar
$ref_to_arr = \@arr; ### \ indicates address of @arr
(OR)
$ref_to_arr{name} =\@arr; ### includes accessing elements of referenced array
####Accessing elements (foreach)###########
foreach $ele (@$ref_to_arr) {
print " Array elements: $ele \n";
}
foreach $ele (@{ $ref_to_arr{name} }) {
print " Array elements: $ele \n";
}
####Accessing elements (for)###########
for ($i=0; $i <= $#$ref_to_arr; $i++) {
print "Array element: $ref_to_arr->[$i] \n";
}
for($i=0; $i <= $#{ $ref_to_arr{name} }; $i++) {
print "Array element: $ref_to_arr{name}{$i} \n";
}
@arr = qw(apple orange banana);
##reference to this array is ==> ref_to_arr scalar
$ref_to_arr = \@arr; ### \ indicates address of @arr
(OR)
$ref_to_arr{name
####Accessing elements (foreach)###########
foreach $ele (@$ref_to_arr) {
print " Array elements: $ele \n";
}
foreach $ele (@{ $ref_to_arr{name
print " Array elements: $ele \n";
}
####Accessing elements (for)###########
for ($i=0; $i <= $#$ref_to_arr; $i++) {
print "Array element: $ref_to_arr->[$i] \n";
}
for($i=0; $i <= $#{ $ref_to_arr{
print "Array element: $ref_to_arr{name
}
Sunday, July 18, 2010
Subscribe to:
Posts (Atom)