title: how to output into a tree like structure via Tree::DAG_Node?
body: <p>I'm trying to write a program that can read from a file then put into a heap sort then output in a tree along with the sorted list. But I got stuck, instead of having the put be the numbers used in the heap sort, it just post 1-10 on one line then 1-9 on the next line. I'm really confused and I barely understand this language as it is. I put a example of the out below alone with the output I am hoping to have it draw. </p>
  
  <pre><code>#!/usr/bin/perl
  use 5.006;
  use strict;
  use warnings;
  use Tree::DAG_Node;
  
  
  process_data(read_file('testing.txt'));
  process_data((3,1,4,1,5,9,2,6,5,3,6));
  
  sub read_file{
      my($filename)=@_;
      my @data=();
      my @words;
      open(my $fh, "&lt;", $filename)
          or die "Could not open file: $!\n";
      while(&lt;$fh&gt;){
      chomp;
      @words = split(' ');
      foreach my $word(@words){
      push @data, $word;
  }
  
  }
  close $fh;
  
  return @data;
  }
  
  
  sub heap_sort {
      my ($a) = @_;
      my $n = @$a;
      for (my $i = ($n - 2) / 2; $i &gt;= 0; $i--) {
          down_heap($a, $n, $i);
      }
      for (my $i = 0; $i &lt; $n; $i++) {
          my $t = $a-&gt;[$n - $i - 1];
          $a-&gt;[$n - $i - 1] = $a-&gt;[0];
          $a-&gt;[0] = $t;
          down_heap($a, $n - $i - 1, 0);
      }
  }
  
  sub down_heap {
      my ($a, $n, $i) = @_;
      while (1) {
          my $j = max($a, $n, $i, 2 * $i + 1, 2 * $i + 2);
          last if $j == $i;
          my $t = $a-&gt;[$i];
          $a-&gt;[$i] = $a-&gt;[$j];
          $a-&gt;[$j] = $t;
          $i = $j;
      }
  
  
  sub max {
      my ($a, $n, $i, $j, $k) = @_;
      my $m = $i;
      $m = $j if $j &lt; $n &amp;&amp; $a-&gt;[$j] &gt; $a-&gt;[$m];
      $m = $k if $k &lt; $n &amp;&amp; $a-&gt;[$k] &gt; $a-&gt;[$m];
      return $m;
  }
  }
  
  sub draw_tree{
      my(@data)=@_;
      my $root = Tree::DAG_Node-&gt;new;
  $root-&gt;name($_[0]);
  $root-&gt;new_daughter-&gt;name($_) for (1..10);
  my @names = @data;
  my $count = 50;
  for my $n ($root-&gt;daughters) {
      for (split //, $names[$count++]) {
          $n-&gt;new_daughter-&gt;name($_)
      }
  }
  print map "$_\n", @{$root-&gt;draw_ascii_tree};
  }
  
  sub process_data{
      my(@data)=@_;
      my @a = @data;
      print "@a\n";
      print "\n";
      heap_sort(\@a);
      draw_tree(@a);
      print "\n";
      print "@a\n";
      print "\n";
  
  }
  </code></pre>
  
  <p>here is the output I get 
  <a href="https://i.stack.imgur.com/CfZDj.png" rel="nofollow noreferrer">CMD output of my code</a></p>
  
  <p>Output I am expecting:
  (using example data)</p>
  
  <p>1 1 9 4 5</p>
  
  <pre><code>   1
     |
   1 ----- 9
   |      
   /\ 
  4 5
  </code></pre>
  
  <p>1 1 4 5 9</p>