 |
| |
How to use the Perl join function with an AOA (array of arrays)
NEW!!!
Check my new resource: Perl How To Tutorial eBooks
Perl join function is used to concatenate the elements of an array or a list into a string, using a separator given by a scalar variable value. For a simple array, I remind you the usual syntax form:$string = join EXPR, LIST;
where:- EXPR may be any string and it represents a separator for the elements of the list or array
- LIST represents a list or array whose elements will be merged into a string
If you have an array of arrays (@AOA), you can use some additional syntax forms for the join function.I’ll illustrate this by giving you a few examples.
Join into a string the elements of a particular sub-array To join into a string the elements of a sub-array, you can use the following syntax form: $string = join EXPR, @ { $AOA [ $index ] }; where $index is the index or subscript of the given sub-array.$AOA[$index] is a reference to a sub-array, so you need to dereference it by putting a @ sign in front of the reference; to avoid any ambiguity or operators precedence rule, the reference is enclosed by braces. Here is an example: #!/usr/local/bin/perl
use strict;
use warnings;
my @AOA = (
[ 'a1', 'b1', 1 ],
[ 'a2', 'b2', 2 ],
[ 'a3', 'b3', 3 ],
[ 'a4', 'b4', 4 ],
);
my $index = 1; # the second sub-array
my $str = join ',', @ { $AOA [ $index ]} ;
print "$str\n";
# it prints: a2,b2,2
The [] is the array constructor and returns a reference to the list enclosed in the square brackets. To join the elements of the subarray, the comma character was used.
Join the elements of a particular column of a matrix into a stringIf you have a matrix represented as an array of arrays, than you can extract the elements of a particular column and join them into a string, by using map and join: $str = join EXPR, map $_->[$index], @AOA;
where $index is the index of the matrix column.@AOA contains array references, the map function will extract for each reference assigned to $_ the corresponding element of the $index column; for dereferencing the -> operator was used because is more easier to read (if you prefer you can use also the notation $$_[$index]). The join function will join the elements of the list returned by map into a string, separating them by EXPR. Here is an example: #!/usr/local/bin/perl
use strict;
use warnings;
my @AOA = (
[ 'a1', 'b1', 1 ],
[ 'a2', 'b2', 2 ],
[ 'a3', 'b3', 3 ],
[ 'a4', 'b4', 4 ],
);
my $index = 1; # the second column of the matrix
$str = join ',', map $_->[$index], @AOA;
print "$str\n";
# it prints: b1,b2,b3,b4
As you can see from the output, the $str variable contains the elements of the matrix second column, separated by comma.
Turn each subarray into a string and get an array with all these stringsIf you have an array of arrays, you can use foreach to iterate through its array references and for each sub-array you can join its elements into a string using an EXPR separator and next append this string into an array using the push function. You can use either an explicit foreach loop with a block or the short form of foreach as a modifier. See below the syntax forms in both cases: foreach my $item (@AOA) {
push @array, join EXPR, @{$item};
}
# or
push @array, join EXPR, @$_ foreach (@AOA);
$item is a reference to an array and because the second argument of the join function is a list or an array, you need to dereference it by using the @{$item} notation.To be more explicit, let’s look at an example: #!/usr/local/bin/perl
use strict;
use warnings;
my @AOA = (
[ 'a1', 'b1', 1 ],
[ 'a2', 'b2', 2 ],
[ 'a3', 'b3', 3 ],
[ 'a4', 'b4', 4 ],
);
my @array = ();
push @array, join ',', @$_ foreach (@AOA);
print "$_\n" foreach @array;
I used the same @AOA as in the examples presented above. I used the foreach loop to print the elements of the resulting array, each element on a new line.The output is as follows: a1,b1,1
a2,b2,2
a3,b3,3
a4,b4,4
NEW!!!
Do you want more information about the basic Perl topics?
Check my new "Perl How To" Tutorial eBooks page where I'll answer the most frequent questions regarding some topics :
Perl How To Tutorial eBooks
Would you like to create your own website like this one? Hit the Alarm Clock!

|
|