I have realtime data streaming into my perl script, and I want to keep a sorted listbox of some element of this stream. The attached code shows a snippet of the first thing that jumped into my mind (that works), but I don't like the fact that if the user selects a listbox item, it is lost when the listbox is next updated.
Then I started fiddling with being able to tie the listbox to a perl list variable, but got hopelessly confused (and figured you'd loose the selection in this scenario as well).
Now I'm thinking that I'll have to do a binary-search-and-insert on the listbox, instead of my bull-in-the-china-shop method of removing, sorting, and restoring the whole list.
I feel like I'm missing something obvious... Any thoughts are much appreciated!
Thanks
-Craig
use strict; use Tk; use Data::Dumper; my @data = (qw( ccc eee ddd aaa aaa ccc ddd ddd bbb ddd eee ggg fff)); my %seen; # Create & configure text widget... my $top = MainWindow->new; my $list = $top->Scrolled('Listbox')-> pack(-side=>'right', -fill=>'both', -expand =>1); my $ref; $ref = $top->repeat(700, \&addItem); sub addItem { my $item = shift(@data); if(!$item) { $ref->cancel; return }; print STDERR "$item\n"; if(! $seen{$item}) { $list->delete(0, 'end'); $list->insert('end', sort(keys(%seen))); } $seen{$item}++; print STDERR "SEEN DUMP:\n", Dumper(\%seen), "\n"; } MainLoop();
In reply to Sorted Realtime Tk Listbox by cmv
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |