11. Array: how-to

1. How to extend an anonymous array?


$mcArrayRef = [a, b, c]; # $mcArrayRef is a reference to an anonymous array    
push(@{$mcArrayRef}, 'd'); # character 'd' is added at the end of the array
2. How to extend an anonymous array conditionally?

$mcArrayRef = [a, b, c]; # $mcArrayRef is a reference to an anonymous array    
 # character 'b' is added at the beginning of the array only if the original array did not contain 'b'
unshift(@{$mcArrayRef}, 'b') if (! grep{ /'b'/ } @{$mcArrayRef});
3. How to find the items of an argument containing an anonymous array?
Dereference !

sub passed_anonymous_arrays {

# $aa_1 and $aa_2 are references	
	($aa_1, $aa_2) = @_;

# create a new array by dereferencing  
	@aar_1 = @{$aa_1};
	print("All elements of the first array: @aar_1\n"); # output: 1 2 3 4
  
# without an extra array
	print("the third element of the second array: $$aa_2[2]\n"); # output: 7
	print("alternative: the third element of the second array: $aa_2->[2]\n"); # output: 7
	
# transverse the array
	foreach (@{$aa_1}) { print($_ . "\n"); } # output: 1 2 3 4 on separate lines
	
# alternative	
	do { print($_ ."\n"); } for (@{$aa_2}); # output: 5 6 7 8 on separate lines		
}

passed_anonymous_arrays([1, 2, 3, 4], [5, 6, 7, 8]);
4. How to create a hash from an array?
a. Assuming the data in @array are stored as key, value, key, value
 
@array = qw(a 0 b 1 c 2);
%hash = ();

%hash = @array;
b. Assuming the data in @array are only keys and the values are values from 0 .. $#array:
 
@array = qw(a b c);
%hash = ();

$hash{$array[$_]} = $_ for (0 .. $#array);
A more flexible solution:

c. Assuming the data in @array_1 are keys and the data in @array_2 are values and both arrays have the same length:
 
@array_1 = qw(a b c);
@array_2= qw(x y z);
%hash = ();

$hash{$array_1[$_]} = $array_2[$_] for (0 .. $#array_1);
To print the hash of the previous code snippets:
 
foreach $key (keys(%hash)) {
  print("$key => $hash{$key}\n");
}

5. Don't forget parentheses!
...it took me too long to find the problem... I wrote


$ref_array = @_;
instead of

($ref_array) = @_;
A complete example with alternatives

@arr = qw( Daniel Florence Sebastian );

sub extract_first_characters {
	
 ($ref_array) = @_; # don't forget the parentheses!!!
 return join('', (map { substr($_, 0, 1) } @$ref_array)) . "\n";	

# also ok
#  $ref_array = shift; # no parentheses required!	
#  return join('', (map { substr($_, 0, 1) } @$ref_array)) . "\n";	
  
# not recommended
#  $ref_array = $_[0];	
#  return join('', (map { substr($_, 0, 1) } @$ref_array)) . "\n";	
 
}
print(extract_first_characters(\@arr)); # output: DFS
With an anonymous array:

sub extract_first_characters {

  ($ref_array) = @_; # don't forget the parentheses!!!
  return join('', (map { substr($_, 0, 1) } @$ref_array)) . "\n";	

}
print(extract_first_characters( [qw( Daniel Florence Sebastian )] )); # output: DFS
A acronym function:

sub acronym {

  ($ref_array) = @_; # don't forget the parentheses!!!
  return join('', (map { substr($_, 0, 1) } @$ref_array));	
}

((acronym([ qw(Create Access Print Iterate Operate) ])) eq "CAPIO") ? print("True\n") : print("False\n");
6. How to find the items of an argument containing an anonymous array?
Dereference !

sub passed_anonymous_arrays {

# $aa_1 and $aa_2 are references	
	($aa_1, $aa_2) = @_;

# create a new array by dereferencing  
	@aar_1 = @{$aa_1};
	print("All elements of the first array: @aar_1\n"); # output: 1 2 3 4
  
# without an extra array
	print("the third element of the second array: $$aa_2[2]\n"); # output: 7
	print("alternative: the third element of the second array: $aa_2->[2]\n"); # output: 7
	
# transverse the array
	foreach (@{$aa_1}) { print($_ . "\n"); } # output: 1 2 3 4 on separate lines
	
# alternative	
	do { print($_ ."\n"); } for (@{$aa_2}); # output: 5 6 7 8 on separate lines		
}

passed_anonymous_arrays([1, 2, 3, 4], [5, 6, 7, 8]);