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 :
- Be careful of the hlist separator, which you chose to be '/', whereas some of your data contains such a character. In my script, I've chosen '!' to be the hlist separator. It is used to build the tree. The '/' is classical as it corresponds to a file system tree.
- I use 'use Tk;' to import the whole functions of the module.
- I use a hash reference to build a internal representation of your tree and then go through it to be the hlist
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
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.