NAME
List::Tuples - Makes tuples from lists
SYNOPSIS
tuples[2] => (1 .. 6) ;
gives :
tuples
|- 0
| |- 0 = 1
| `- 1 = 2
|- 1
| |- 0 = 3
| `- 1 = 4
`- 2
|- 0 = 5
`- 1 = 6
ref_mesh([1 .. 3], ['a' .. 'b'], ['*']) ;
gives :
ref_mesh
|- 0 = 1
|- 1 = a
|- 2 = *
|- 3 = 2
|- 4 = b
|- 5 = undef
|- 6 = 3
|- 7 = undef
`- 8 = undef
hash_tuples ['key', 'other_key'] => (1 .. 5) ;
gives :
hash_tuples
|- 0
| |- key = 1
| `- other_key = 2
|- 1
| |- key = 3
| `- other_key = 4
`- 2
|- key = 5
`- other_key = undef
# note: output generated by Data::TreeDumper
DESCRIPTION
This module defines subroutines that let you create tuples.
DOCUMENTATION
Ever got frustrated that you couldn't easily get tuples into a map{} or create multiple hashes from an ordered list?
Jonathan Scott in In "Everyday Perl 6" http://www.perl.com/pub/a/2007/05/10/everyday-perl-6.html writes:
# Perl 6 # Perl 5
for @array -> $a { ... } for my $a (@array) { ... }
for @array -> $a, $b { ... } # too complex :)
The following subroutines will simplify your job. They could certainly be more effective implemented directly in the language, IE in Perl6. If you have millions of tuples to handle, you may want monitor memory usage.
SUBROUTINES
tuples([$limit], \@size, @list)
tuples will extract $size elements from @lists and group them in an array reference. It will extract as many tuples as possible up to the, optional, $limit you pass as argument.
tuples 3 => [2] => (1 .. 14); # 3 tuples with 2 elements are returned
tuples[2] => (1 .. 14); # as many tuples with 2 elements are returned
for my $tuple (tuples[2], @array)
{
print "[$tuple->[0], $tuple->[1]]\n" ;
}
Arguments
- $limit - an optional maximum number of tuples to create
- \@size - an array reference containing the number of elements in a tuple
- @list - a list to be split into tuples
Return
A list of tuples (array references)
Input list with insufficient elements
tuples[2] => (1 .. 3)) ;
gives :
tuples
|- 0
| |- 0 = 1
| `- 1 = 2
`- 1
`- 0 = 3
ref_mesh(\@array1, \@array2, ...)
Mixes elements from arrays, one element at the time.
ref_mesh
['mum1', 'mum2', 'mum3'],
['dad1', 'dad2'],
[['child1_1', 'child1_2'], [], ['child3_1']] ;
gives the folowing list:
|- 0 = mum1
|- 1 = dad1
|- 2
| |- 0 = child1_1
| `- 1 = child1_2
|- 3 = mum2
|- 4 = dad2
|- 5 (no elements)
|- 6 = mum3
|- 7 = undef
`- 8
`- 0 = child3_1
This is equivalent to mesh from List::MoreUtils except the fact it takes arrays references instead for lists. The implementation is directly taken from List::MoreUtils.
Arguments
a list of array reference
Return
a list consisting of the first elements of each array reference, then the second, then the third, etc, until all arrays are exhausted
hash_tuples([$limit], \@hash_keys, @input_array)
hash_tuples uses elements from \@input_array and combine them with \@hash_keys to create hash references. It will create as many hashes as possible up to the, optional, $limit.
hash_tuples
['Mum', 'Dad', 'Children'] =>
'Lena', 'Nadim', ['Yasmin', 'Miriam'],
'Monika', 'ola', ['astrid'] ;
gives:
|- 0
| |- Children
| | |- 0 = Yasmin
| | `- 1 = Miriam
| |- Dad = Nadim
| `- Mum = Lena
`- 1
|- Children
| `- 0 = astrid
|- Dad = ola
`- Mum = Monika
for my $tuple (hash_tuples(['a', 'b'] => @array))
{
print $tuple->{a} . "\n" ;
print $tuple->{b} . "\n" ;
}
Arguments
- $limit - an optional maximum number of hashes to create
- \@hash_keys - an array reference containing the list of keys apply to the input array
- \@input_array- an array reference. the array contains the elements to extract
Return
A list of hashes
BUGS AND LIMITATIONS
None so far.
AUTHOR
Khemir Nadim ibn Hamouda
CPAN ID: NKH
mailto:nadim@khemir.net
LICENSE AND COPYRIGHT
This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
SUPPORT
You can find documentation for this module with the perldoc command.
perldoc List::Tuples
You can also look for information at:
AnnoCPAN: Annotated CPAN documentation
RT: CPAN's request tracker
Please report any bugs or feature requests to L <bug-list-tuples@rt.cpan.org>.
We will be notified, and then you'll automatically be notified of progress on your bug as we make changes.
Search CPAN