onlyforbopi
12/29/2016 - 6:06 PM

PERL_LISTS_ARRAYS.pl

PERL_LISTS_ARRAYS.pl

# Total length of an array of strings.  Adds up string lengths.
# -> Reference to array
# <- Total string length
sub totlen
{
    my $len= 0;
    map { $len += length($_); } @_;
    return $len;
}

my @a = ( 'aaa', 'bbb' );
 
print totlen(@a);
my @list = ( 1, 2, 3 );
 
sub sumarray{
	my @arr = @_;
	#print @arr;
	my $total = 0;
	#print $total;
	foreach my $x (@arr) {
		$total += $x;
		#print $total;
	}
	return $total;
 
}
 
 
print sumarray(@list);
use Data::Dumper qw(Dumper);

sub filter_list {
  
  # Function : 
  # Description : 
  # Input : 
  # Output : 
  # Usage : 
  
    my $name = shift;
    my @arrayout;
    foreach my $element (@_)
    {
       if (!($element eq $name)){
       	push @arrayout, $element;
       }
    }
    return @arrayout
}
 
@array = ( 'Tom', 'John', 'Mary' );
print filter_list('Tom',@array);
 
@array2 = filter_list('Tom',@array);
 
print Dumper \@array2;
#!/usr/bin/perl
# your code goes here
use Data::Dumper;

sub uniq {
  
  # Function : 
  # Description : 
  # Input : 
  # Output : 
  # Usage : 
  
    my %h;
    return grep { !$h{$_}++ } @_
}


@a = ( 1, 2, 3, 4, 5, 4, 3, 2, 1);
@c = ( 'aa' , 'bb' , 'aa' , 'cc' );

%b = uniq(@a);
%d = uniq(@c);
#print %b;
#print %d;

print "@{[%b]}";


################################################


sub uniq {
    my %seen;
    grep !$seen{$_}++, @_;
}

my @array = qw(one two three two three);
my @filtered = uniq(@array);

print "@filtered\n";


#################################################


use strict;
use warnings;
use List::MoreUtils qw(uniq);

my @dup_list = qw(1 1 1 2 3 4 4);

my @uniq_list = uniq(@dup_list);

##################################################

y @arr;
my @uniqarr;

foreach my $var ( @arr ){
  if ( ! grep( /$var/, @uniqarr ) ){
     push( @uniqarr, $var );
  }
}
##################################################

my %unique = ();
foreach my $item (@myarray)
{
    $unique{$item} ++;
}
my @myuniquearray = keys %unique;


###################################################

my @list = ( 1, 2, 3, 4 );
 
sub prodarray{
	my @arr = @_;
	#print @arr;
	my $total = 1;
	#print $total;
	foreach my $x (@arr) {
		$total *= $x;
		#print $total;
	}
	return $total;
 
}
 
 
print prodarray(@list);
#!/usr/bin/perl
# your code goes here


use Data::Dumper qw(Dumper);

my @first  = (7, 8);
my @second = (7, 8, 5);
#add(\@first, \@second);  # passing two references
 



sub filter_list {
  
  # Function : 
  # Description : 
  # Input : 
  # Output : 
  # Usage : 
	
	my ($one_ref, $two_ref) = @_;
    my @filter = @{ $one_ref };       # dereferencing and copying each array
    my @base = @{ $two_ref };
    my @arrayout;
    foreach my $element (@base)
    {
       if (!($element ~~ @filter )){
       	push @arrayout, $element;
       }
    }
    return @arrayout
}

@filter = ( 'Tom', 'John' ); 
@array = ( 'Tom', 'John', 'Mary' );
@array3 = filter_list(\@filter,\@array);
print @array3;
print "\n";
# Maximum of a list.
sub max
{
    return undef unless @_;
    my $max= shift;
    for (@_) { $max= $_ if $_ > $max; }
    return $max;
}



my @a = ( 1, 2, 3, 4 );
print max(@a);
my @array1  = (1..10, 'string');
my @array2  = (1..10, 'string');
 
my $is_same = is_same(\@array1, \@array2);
 
print "is_same: $is_same\n";
 
sub is_same {
  my($array1, $array2) = @_;
 
  # immediately return false if the two arrays are not the same length
  return 0 if scalar(@$array1) != scalar(@$array2);
 
  # turn off warning about comparing uninitialized (undef) string values
  # (limited in scope to just this sub)
  no warnings;
 
  for (my $i = 0; $i <= $#$array1; $i++) {
    if ($array1->[$i] ne $array2->[$i]) {
      return 0;
    }
  }
 
  return 1;
}
#!/usr/bin/perl
# your code goes here
use warnings;
use strict;
use Data::Dumper;
$Data::Dumper::Sortkeys=1;

#my @names = qw(bob mike joe bob);
#my %counts;
#$counts{$_}++ for @names;
#print Dumper(\%counts);

sub count_uniq
{
  # Function : 
  # Description : 
  # Input : 
  # Output : 
  # Usage : 
  
	my @names = @_;
	#print @names;
	my %counts;
	$counts{$_}++ for @names;
	return %counts;
}

my @a = ( 1, 1, 1, 2, 3, 3, 3, 4, 4, 5 );
my %c = count_uniq(\@a);
print Dumper(\%c);

__END__
sub concat
{
  # Function : concat 
  # Description : Concatenates two lists
  # Input : 2 arrays/lists
  # Output : 1 concatenated
  # Usage : my @a = concat(@b, @c ); (See Below) 
  
  my @result;
 
  map { push @result, @$_; } @_;
  return @result;
}
 
my @b = [1,2,3,4,5,6];
my @c = [1,2,3,4,5,6];
 
my @a = concat(\@b, \@c );
print @a
1. concat           = Concatenate Lists
2. rdupl_list       = Remove duplicate elements from list
3. count_dupl_list  = Count duplicate elements in list
4. reverse_array    = Reverse an array
5. single_filter    = Filters one value out of array
6. multiple_filter  = Filters an array of values out of another
7. flatten_array    = Flattens an array
8. sum_of_array     = Calculates the sum of all array elements
9. prod_of_array    = Calculates the product of all array elements
10.max_element      = Returns the max element of an array
11.min_element      = Retuns the smallest of elements in array
12.pretty_print     = Pretty prints an array
13.total_length     = Calculates total length of array of strings (sums)
14.is_same          = Checks if two arrays are the same
sub rev
{
  # Function : 
  # Description : 
  # Input : 
  # Output : 
  # Usage : 
  
    my @r;
    push @r, pop @_ while @_;
    @r
}
 
 
my @a = ( 1, 2, 3, 4, 5 );
 
print rev(@a);
my @a = ( 1, 2, 3, 4 );

#print join(", ", @a);

sub prettyarray {
	
	print "( ";
	print join(", ", @_);
	print " )";
	print "\n";
	
	return 0
}

print prettyarray(@a);
sub flatten {
    map { ref eq 'ARRAY' ? flatten(@$_) : $_ } @_
}
 
my @lst = ([1], 2, [[3,4], 5], [[[]]], [[[6]]], 7, 8, []);
print flatten(@lst), "\n";