in reply to how extract and group data in a array
I'm not aware of any module to do this.
I don't see how "recursion" would help you here.
There's no need for an "ugly" loop. Just iterate your array; use a boolean flag to indicate if elements are in the wanted group.
#!/usr/bin/env perl -l use strict; use warnings; my @dd = qw/AA JJ CC M 1 1 3 4 D JJ 1 1 D M 3 3 4 D C M 3 3 3 D P Z/; my $in_group = 0; my (@group, @all_groups); for (@dd) { $in_group = 1 if $_ eq 'M'; next unless $in_group; push @group, $_; if ($_ eq 'D') { push @all_groups, [ @group ]; @group = (); $in_group = 0; } } print "@$_" for @all_groups;
Output:
M 1 1 3 4 D M 3 3 4 D M 3 3 3 D
— Ken
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: how extract and group data in an array
by Athanasius (Archbishop) on Feb 21, 2017 at 07:45 UTC | |
|
Re^2: how extract and group data in a array
by Anonymous Monk on Feb 21, 2017 at 07:18 UTC | |
by kcott (Archbishop) on Feb 21, 2017 at 21:30 UTC |