in reply to Displaying a list of hashes in TK in a Hierarchical folder presentation

Here is a solution to your problem :

use strict; use vars qw/%hash/; use Tk; use Tk::TList; use Tk::HList; my $mw = MainWindow->new(); my $image = $mw->Getimage('folder'); my $tlist = $mw->TList(-orient => 'vertical'); my $label = $mw->Label(-width=>15); my $hlist = $mw->HList( -itemtype => 'text', -separator => '!', -selectmode => 'single', -browsecmd => sub { my $file = shift; $label->configure(-text=> +$file); }); chomp (my @work_file=<DATA>); for (@work_file) { next if ($_ !~ m!,!); @data = split (/[,\s]+/,$_); $ref = \%hash; foreach (@data) { $ref->{$_} = {} if (not defined $ref->{$_}) $ref = $ref->{$_}; } } foreach my $bu (keys %hash) { $hlist->add($bu, -text=>$bu); foreach my $group (keys %{$hash{$bu}}) { $hlist->add("$bu!$group", -text => $group); foreach my $function (keys %{$hash{$bu}->{$group}}) { $hlist->add("$bu!$group!$function", -text => $function); } } } $hlist->pack; $label->pack; MainLoop; __DATA__ Dept Group Function IT, Projects, SAN/NAS IT, Development, GUI IT, Security, HR, Admin, West HR, Management, West Legal, Admin, FirmWide Legal, Compliance,

There are several things to say :


Some things should be improved though. For example, one could use only one pass to create the hlist, that is build the hash and the hlist is the same loop.

HTH

--
zejames
  • Comment on Re: Displaying a list of hashes in TK in a Hierarchical folder presentation
  • Download Code

Replies are listed 'Best First'.
Re^2: Displaying a list of hashes in TK in a Hierarchical folder presentation
by blackadder (Hermit) on Sep 14, 2004 at 12:41 UTC
    Thats cool,...But still not what I am after,...

    If I create a folder on my C drive and call it IT, then inside this folder I create as many folders as as IT has Groups (i.e. I'd creat a folder for Projects, Development,...etc) then inside all these sub folders I would create as many folders as IT Group functions have (i.e. inside Projects folder I create a folder for SAN/NAS and inside developments folder I'd create a folder GUI,...etc.) Then finallt, I'd use Tk::DirTree to represent those folder. This is the way I am after. Is this possible to achieve with some help from you Guys?

    I am sweating over it now guys.

    Thanks
    Blackadder
      In fact that is what my code do. Could you tell us why it does not fit your needs ?

      Kind regards

      --
      zejames
        Ok,..Please bear with me - maybe I ma not making myself clear..

        For a start your code doesn't represent the data in Dirtree like display. Your code displays the data , although its in hierarchal manner, buts its more like a table.

        Lets forget I have a C drive and instead lets assume I have a drive called 'London', inside this drive I created folders that I called IT, HR, Legal, IBD,...etc. Iside each of these folders I created my Groups and then my function units. Then I programmed a Tk::DirTree to point to this drive. So in my Tk window I should see a top level folder called 'London' this folder will show as an opened folder icon. Under it I should see 4 folders with a plus sign on them to indicate that there are other folder inside them. exactly like this code;
        use strict; use Tk 800.005; use Tk::TList; use Tk::DirTree; use Tk::Frame; use Tk::Scrollbar; use Tk::Adjuster; use vars qw/%tk %dr %im @users @groups $dnd_token/; $tk{mw} = MainWindow->new(-background => 'white'); $tk{mw}->geometry('500x400'); $tk{left_frame} = $tk{mw}->Frame; $tk{adjuster} = $tk{mw}->Adjuster(-widget=> $tk{left_frame}, -side= +>'left'); $tk{right_frame} = $tk{mw}->Frame; $tk{dir_tree} = $tk{left_frame}->Scrolled('DirTree', -height=>'0', -width=>'0', -scrollbars=>'e', ); $tk{output_list} = $tk{right_frame}->Scrolled('TList', -height=>'1', -width=>'1', -scrollbars=>'osoe',); $tk{user_list} = $tk{right_frame}->Scrolled('TList', -height=>'1', -width=>'1', -scrollbars=>'osoe',); $tk{group_list} = $tk{right_frame}->Scrolled('TList', -height=>'1', -width=>'1', -scrollbars=>'osoe',); $tk{user_label} = $tk{right_frame}->Label(-text => "Source Machines" +); $tk{group_label} = $tk{right_frame}->Label(-text => "Applications"); $tk{output_label} = $tk{right_frame}->Label(-text => "WinBcp Hosts"); $tk{dir_tree}-> delete('all'); $tk{dir_tree}-> chdir ("c:\\"); $tk{left_frame}->pack(qw/-side left -fill y/); $tk{adjuster}->pack(qw/-side left -fill y/); $tk{right_frame}->pack(qw/-side right -fill both -expand 1/); $tk{dir_tree} ->pack(qw/-side left -fill both -expand 1/); $tk{output_label}->pack(qw/-side top -fill both/); $tk{output_list} ->pack(qw/-side top -fill both -expand 1/); $tk{user_label} ->pack(qw/-side top -fill both/); $tk{user_list} ->pack(qw/-side top -fill both -expand 1/); $tk{group_label} ->pack(qw/-side top -fill both/); $tk{group_list} ->pack(qw/-side top -fill both -expand 1/); MainLoop; exit 0;
        As you can see from the left frame, the layout of the data (represented in a folder form) is what I am trying to achieve here. So instead of the folders on my C drive I need to display the Departemental data in the same manner.

        Thanking You

        Blackadder