Problem in generating ids through Array
Anonymous Monk
created: 2004-06-15 09:01:08

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.

Re: Problem in generating ids through Array
created: 2004-06-15 09:12:52

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?

Re: Problem in generating ids through Array
created: 2004-06-15 09:14:17
Might help if you told folks what you think the rules are for the conversion.

Dave.

Re^2: Problem in generating ids through Array
created: 2004-06-15 09:20:02

I believe we are looking at an indentation level -> paragraph numbering algorithm, unless the OP states otherwise.

Re: Problem in generating ids through Array
hv
created: 2004-06-15 09:22:32

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

Re: Problem in generating ids through Array
created: 2004-06-15 23:02:44
@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