On Sat, Aug 1, 2015 at 12:39 PM, Simon Reinhardt <si...@keinstein.org> wrote:
> Am 30.07.2015 um 20:42 schrieb Brandon McCaig:
>> I'll give you my 2 cents for whatever that's worth. >:)
>
> Thanks for your many comments.
>
> The updated full project code is below.  Feedback is appreciated.  (you
> also find this at github.com/simon0x5b/bookmark-djvu)
>
>> I can't help thinking that the entire recursive subroutine could
>> be simplified and tidied up a bit by changing the direction of
>> it
> could you elaborate on this?

Generally I meant that your solution was short and simple enough that
it probably wasn't worth the extra effort of trying to improve it
[unless the code was going to be maintained for a long time]. This is
what I came up with in a few minutes. It appears to work, but I
reserve the right to be missing something.

#!/usr/bin/env perl

use strict;
use warnings;

use Data::Dumper;

my @input = (
    {
        title => 'title 1',
        level => 1,
        page => 1,
    },
    {
        title => 'title 1.1',
        level => 2,
        page => 2,
    },
    {
        title => 'title 1.2',
        level => 2,
        page => 3,
    },
    {
        title => 'title 1.2.1',
        level => 3,
        page => 4,
    },
    {
        title => 'title 2',
        level => 1,
        page => 5,
    },
);

my @output = inflate_outline_tree(\@input);

print Dumper \@output;

sub inflate_outline_tree {
    my ($input) = @_;
    my @output;
    my %prev = (1 => { kids => \@output });

    for my $flat_node (@$input) {
        my ($level, $page, $title) = @{$flat_node}{qw/level page title/};
        my $node = {page => $page, title => $title};

       push @{$prev{$level}{kids}}, $node;
       $prev{$level + 1} = $node;
    }

    return \@output;
}

__END__

Regards,


-- 
Brandon McCaig <bamcc...@gmail.com> <bamcc...@castopulence.org>
Castopulence Software <https://www.castopulence.org/>
Blog <http://www.bambams.ca/>
perl -E '$_=q{V zrna gur orfg jvgu jung V fnl. }.
q{Vg qbrfa'\''g nyjnlf fbhaq gung jnl.};
tr/A-Ma-mN-Zn-z/N-Zn-zA-Ma-m/;say'

-- 
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/


Reply via email to