Hi monks,
I want to convert an array as given below in to
input:
@a=(1, 2, 3, 3, 2, 1, 2, 2)
Output:
@out=(1, 1.1, 1.1.1, 1.1.2, 1.2, 2, 2.1, 2.2)
Please Kindly help me to do this.
Million thanx in advance.
Homework problem, huh? No code.
You have the same number of outputs as inputs, which is a clue to the fact that you should be looping over the input. If you have an array of counters, one per level (0, 0, 0), then every time around the loop, the value tells you which level counter to increment. If the value decreases, zero the counter one level below. Then all you need to do is print out the level counters each time, ignoring any that are zero.
Does that help?
Dave.
I'd go for something like:
my @s = ();
local $" = '.';
@out = map { $#s=--$_; ++$s[$_]; "@s" } @a;
except that I'd do it in a way that didn't modify the source array. Of course, if this were homework it would probably be better to write it in a way that showed I understood what I was doing. :)
Hugo
@a=(1, 2, 3, 3, 2, 1, 2, 2);
my @c = ();
my $last = 0;
for my $i(@a) {
$c[$i+1] = 0 if $i < $last;
$c[$i]++;
$last = $i;
push @out, join '.', grep{$_} @c;
}
print "$_\n" for @out;
cheers
tachyon
perlmonks.org content © perlmonks.org and Anonymous Monk, dave_the_m, hv, pbeckingham, tachyon
prlmnks.org © 2006 edmund von der burg (eccles & toad)
v 0.03