1. How to extend an anonymous array?
2. How to extend an anonymous array conditionally?$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
3. How to find the items of an argument containing an anonymous array?$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});
4. How to create a hash from an array?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]);
b. Assuming the data in @array are only keys and the values are values from 0 .. $#array:@array = qw(a 0 b 1 c 2); %hash = (); %hash = @array;
A more flexible solution:@array = qw(a b c); %hash = (); $hash{$array[$_]} = $_ for (0 .. $#array);
To print the hash of the previous code snippets:@array_1 = qw(a b c); @array_2= qw(x y z); %hash = (); $hash{$array_1[$_]} = $array_2[$_] for (0 .. $#array_1);
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
instead of$ref_array = @_;
A complete example with alternatives($ref_array) = @_;
With an anonymous array:@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
A acronym function: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
6. How to find the items of an argument containing an anonymous array?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");
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]);