Home > Archive > PERL Beginners > October 2005 > Enlisting All Possible Ranges of An Array
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
| Author |
Enlisting All Possible Ranges of An Array
|
|
| Edward Wijaya 2005-10-25, 6:56 pm |
|
Dear Sirs,
Given this array:
my @array = qw ( A B C D E );
I want to enumerate all its possible ranges
throughout the elements. Such that it gives
the following desired answer, that look like this ( I manually crafted it):.
my $ans = [ 'A',
'B',
'C',
'D',
'E',
'AB',
'ABC',
'ABCD',
'ABCDE',
'BCD',
'BCDE',
'CD',
'CDE',
'DE' ];
Later, I also want to extend this
to deal with AoA as an input. But with
the same concept.
Is there an efficient way to achieve it?
I truly do not know how to go about it.
Really hope to hear from you again.
--
Regards,
Edward WIJAYA
SINGAPORE
| |
| Xavier Noria 2005-10-25, 6:56 pm |
| On Oct 25, 2005, at 16:36, Edward Wijaya wrote:
> Given this array:
>
> my @array = qw ( A B C D E );
>
> I want to enumerate all its possible ranges
> throughout the elements.
Math::Combinatorics can help there.
-- fxn
| |
| wisefamily@integrity.com 2005-10-25, 6:56 pm |
|
Edward Wijaya wrote:
> Dear Sirs,
>
> Given this array:
>
> my @array = qw ( A B C D E );
>
> I want to enumerate all its possible ranges
> throughout the elements. Such that it gives
> the following desired answer, that look like this ( I manually crafted it):.
>
> my $ans = [ 'A',
> 'B',
> 'C',
> 'D',
> 'E',
> 'AB',
> 'ABC',
> 'ABCD',
> 'ABCDE',
> 'BCD',
> 'BCDE',
> 'CD',
> 'CDE',
> 'DE' ];
>
> Later, I also want to extend this
> to deal with AoA as an input. But with
> the same concept.
>
> Is there an efficient way to achieve it?
> I truly do not know how to go about it.
> Really hope to hear from you again.
>
> --
> Regards,
> Edward WIJAYA
> SINGAPORE
I think this will work:
my @array = qw(A B C D E);
my $result = [@array];
my($i1, $i2, $i3, @chars);
for($i1 = 0; $i1 <= $#array; $i1++) {
for($i2 = $i1 + 1; $i2 <= $#array; $i2++) {
@chars = ();
for($i3 = $i1; $i3 <= $i2; $i3++) {
push @chars, $array[$i3];
}
push @$result, join '', @chars;
}
}
Given the array at the beginning, $result will be a reference to the
following array:
[
'A',
'B',
'C',
'D',
'E',
'AB',
'ABC',
'ABCD',
'ABCDE',
'BC',
'BCD',
'BCDE',
'CD',
'CDE',
'DE'
]
If you want an array more like this:
[
'A',
'AB',
'ABC',
'ABCD',
'ABCDE',
'B',
'BC',
'BCD',
'BCDE',
'C',
'CD',
'CDE',
'D',
'DE',
'E'
]
then you would change two lines. First change the line
my @results = [@array];
to
my @results = [];
and change the line
for($i2 = $i1 + 1; $i2 <= $#array; $i2++) {
to
for($i2 = $i1; $i2 <= $#array; $i2++) {
Hope this helps,
David
| |
| Chris Charley 2005-10-25, 6:56 pm |
| > Dear Sirs,
>
> Given this array:
>
> my @array = qw ( A B C D E );
>
> I want to enumerate all its possible ranges
> throughout the elements. Such that it gives
> the following desired answer, that look like this ( I manually crafted
> it):.
>
> my $ans = [ 'A',
> 'B',
> 'C',
> 'D',
> 'E',
> 'AB',
> 'ABC',
> 'ABCD',
> 'ABCDE',
> 'BCD',
> 'BCDE',
> 'CD',
> 'CDE',
> 'DE' ];
>
> Later, I also want to extend this
> to deal with AoA as an input. But with
> the same concept.
>
> Is there an efficient way to achieve it?
> I truly do not know how to go about it.
> Really hope to hear from you again.
Hello Edward
The below will produce what you want. Hope this helps.
#!/usr/bin/perl
use strict;
use warnings;
use Data::Dumper;
my @array = qw ( A B C D E );
my @data;
for my $i (0..$#array) {
my $str = '';
for my $j ($i..$#array) {
$str .= $array[$j];
push @data, $str;
}
}
print Dumper \@data;
__END__
*** Output
C:\perlp>perl t6.pl
$VAR1 = [
'A',
'AB',
'ABC',
'ABCD',
'ABCDE',
'B',
'BC',
'BCD',
'BCDE',
'C',
'CD',
'CDE',
'D',
'DE',
'E'
];
C:\perlp>
|
|
|
|
|