Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

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.

  • Comment on Problem in generating ids through Array

Replies are listed 'Best First'.
Re: Problem in generating ids through Array
by pbeckingham (Parson) on Jun 15, 2004 at 13:12 UTC

    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
by hv (Prior) on Jun 15, 2004 at 13:22 UTC

    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
by dave_the_m (Monsignor) on Jun 15, 2004 at 13:14 UTC
    Might help if you told folks what you think the rules are for the conversion.

    Dave.

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

Re: Problem in generating ids through Array
by tachyon (Chancellor) on Jun 16, 2004 at 03:02 UTC
    @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