Folks-

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

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • 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:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.