/ / przekonwertuj wielopoziomowy hash ref do tabeli HTML z rodzicielską relacją potomną w perl - perl, hash, ref

przekonwertować wielopoziomowy hash ref do tabeli HTML z rodzicielską relacją podrzędną w perl - perl, hash, ref

Mam wielopoziomowy refash hash mający relację nadrzędną i podrzędną i chcę przekonwertować ten skrót na prostą tabelę HTML ze wszystkimi relacjami. mój hash wygląda tak:

$VAR1 = {
"4" => {
"forumid" => "136720",
"children" => {
"7" => {
"forumid" => "136997",
"title" => "under category",
"is_category" => "0",
"parentid" => "136720"
}
},
"title" => "Android",
"is_category" => "0",
"parentid" => "-1"
},
"1" => {
"forumid" => "136666",
"children" => {
"5" => {
"forumid" => "136954",
"children" => {
"8" => {
"forumid" => "137004",
"title" => "child of child",
"is_category" => "0",
"parentid" => "136954"
}
},
"title" => "add child",
"is_category" => "0",
"parentid" => "136666"
}
},
"title" => "Main Forum",
"is_category" => "0",
"parentid" => "-1"
},
"3" => {
"forumid" => "136719",
"title" => "Nokia C2-01",
"is_category" => "1",
"parentid" => "-1"
},
"2" => {
"forumid" => "136665",
"children" => {
"6" => {
"forumid" => "136994",
"children" => {
"9" => {
"forumid" => "137012",
"title" => "another child",
"is_category" => "0",
"parentid" => "136994"
}
},
"title" => "sub form under test forum",
"is_category" => "0",
"parentid" => "136665"
}
},
"title" => "test",
"is_category" => "0",
"parentid" => "-1"
}
};

i używam następującej funkcji, aby utworzyć tabelę HTML:

sub adminList {

my $hash = shift;
my $options = "";
my $iter;
$options .= "<table id="sortable" class="grid" >
<tbody>";
$iter = sub {
my $hash = shift;
my $indent = shift || "";
foreach my $k (sort keys %{$hash}) {
my $v = $hash->{$k};
$options .= "<tr><td>$v->{title}" ;
if($indent){
$options .= "<table id="sort">
<tbody>
<tr>
<td> $indent $v->{title}</td>
</tr>
</tbody>
</table>
";
}
if ($v->{children}){
$iter->($v->{children}, $indent . "--");
}
$options .= "</td></tr>";
}
};
$iter->($hash);
$options .="</tbody></table>";
return $options;
}

co nie daje mi pożądanych rezultatów i dwukrotnie powtarzam tytuł zamiast tworzyć relację rodzicielską. Wynik powinien być w formie tabelarycznej:

Main Forum
-- add child
---- child of child
test
-- sub form under test forum
---- another child
Nokia C2-01
Android
-- under category

Nie jestem w stanie uporządkować tego, czego brakuje? wszelka pomoc byłaby doceniana. utworzona tabela powinna wyglądać następująco:

<table id="sortable" class="grid" >
<tbody>
<tr>
<td><label>house-Five</label>
<table id="sort">
<tbody>
<tr>
<td>-- child1111</td>
</tr>
<tr>
<td><label>-- child22222</label>
<table id="sort">
<tbody>
<tr><td>---- first child of child22222</td></tr>
<tr>
<td>---- second child of child22222
<table id="sort">
<tbody>
<tr><td>------ first child of second child222222</td></tr>
<tr><td>------ second child of child22222222</td></tr>
</tbody>
</table>
</td>
</tr>
</tbody>
</table>

</td>
</tr>
<tr>
<td>-- child33333</td>
</tr>
</tbody>
</table>
</td>
</tr>

<tr><td>Psition four</td></tr>
<tr><td>catch me</td></tr>
<tr><td>Champions at six</td></tr>
<tr><td>Rosewater</td></tr>
</tbody>
</table>

Odpowiedzi:

0 dla odpowiedzi № 1

Dwukrotnie dodajesz tytuł. Po prostu zmień następującą linię

$options .= "<tr><td>$v->{title}";

do

 $options .= "<tr><td>$v->{title}" unless $indent;

Możesz dołączyć test do poniższych if, robiąc to jeśli-jeszcze.