in reply to Re: Determing "numeric value" of string?
in thread Determing "numeric value" of string?

Feel free...but I've spent hours on this. Here is my test script, note the section that works and does not work.
use Wx; package MyFrame; use strict; use vars qw(@ISA); @ISA = qw(Wx::Frame); use Wx qw(:everything); my %hash = (); my $sort = 'asc'; my $acol = 0; sub new { my( $class ) = shift; my( $this ) = $class->SUPER::new( undef, -1, $_[0], [ @_[1, 2] ], [ @_[3, 4] ] ); $this->{LISTCTRL} = Wx::ListCtrl->new( $this, -1, wxDefaultPositio +n, wxDefaultSize, wxLC_REPORT ); $this->{LISTCTRL}->InsertColumn( 0, "Column 1" ); $this->{LISTCTRL}->InsertColumn( 1, "Column 2" ); $this->{LISTCTRL}->InsertColumn( 2, "Column 3" ); # control hidden to speed up insertion $this->{LISTCTRL}->Show( 0 ); my( $id ); my( $elements ) = 5; foreach ( 1 .. $elements - 1 ) { # WORKS #my $data = int(rand(100)); #my $data2 = int(rand(100)); #my $data3 = int(rand(100)); # DOES NOT WORK my $data = "STRING: " . int(rand(100)); my $data2 = "STRING: " . int(rand(100)); my $data3 = "STRING: " . int(rand(100)); $id = $this->{LISTCTRL}->InsertStringItem( $_, $data ); $this->{LISTCTRL}->SetItemData( $id, $data ); $this->{LISTCTRL}->SetItem( $id, 1, $data2 ); $this->{LISTCTRL}->SetItem( $id, 2, $data3 ); $hash{$data}{0} = $data; $hash{$data}{1} = $data2; $hash{$data}{2} = $data3; } $this->{LISTCTRL}->Show( 1 ); my( $item ) = Wx::ListItem->new; $this->{LISTCTRL}->SetColumnWidth( 0, wxLIST_AUTOSIZE ); $this->{LISTCTRL}->SetColumnWidth( 1, wxLIST_AUTOSIZE ); $this->{LISTCTRL}->SetColumnWidth( 2, wxLIST_AUTOSIZE ); my( $topsizer ) = Wx::BoxSizer->new( wxVERTICAL ); $topsizer->Add( $this->{LISTCTRL}, 2, wxGROW ); $topsizer->Add( $this->{LOGWINDOW}, 1, wxGROW ); $this->SetSizer( $topsizer ); $this->SetAutoLayout( 1 ); use Wx::Event qw( EVT_LIST_COL_CLICK ); EVT_LIST_COL_CLICK( $this, $this->{LISTCTRL}, \&OnSort ); $this } sub Cmp { my( $f, $s ) = @_; my( $i1, $i2 ) = ( $hash{$f}{$acol}, $hash{$s}{$acol} ); #print "$f => $s\n"; #print "$i1, $i2\n"; if ($sort eq 'asc') { $i1 < $i2; } elsif ($sort eq 'desc') { $i1 > $i2; } } sub OnSort { my( $this, $event ) = @_; $acol = $event->GetColumn; # use Data::Dumper; # print Dumper(%hash); if ($sort eq 'asc') { $sort = 'desc'; } else { $sort = 'asc'; } $this->{LISTCTRL}->SortItems( \&Cmp ); } package MyApp; use strict; use vars qw(@ISA); @ISA = qw(Wx::App); sub OnInit { my( $this ) = @_; my( $frame ) = MyFrame->new( 'wxPerl ListCtrl Test', 50, 50, 450, 34 +0 ); $frame->Show( 1 ); $this->SetTopWindow( $frame ); 1; } package main; my( $app ) = MyApp->new; $app->MainLoop;

Replies are listed 'Best First'.
Re^3: Determing "numeric value" of string?
by getsw (Initiate) on Jun 15, 2009 at 23:20 UTC
    I figured out a solution, since I too am having problems with it. Here's the code which will now sort strings:
    use Wx; package MyFrame; use strict; use vars qw(@ISA); @ISA = qw(Wx::Frame); use Wx qw(:everything); my %hash = (); my $sort = 'asc'; my $acol = 0; sub new { my( $class ) = shift; my( $this ) = $class->SUPER::new( undef, -1, $_[0], [ @_[1, 2] ], [ @_[3, 4] ] ); $this->{LISTCTRL} = Wx::ListCtrl->new( $this, -1, wxDefaultPositio +n, wxDefaultSize, wxLC_REPORT ); $this->{LISTCTRL}->InsertColumn( 0, "Column 1" ); $this->{LISTCTRL}->InsertColumn( 1, "Column 2" ); $this->{LISTCTRL}->InsertColumn( 2, "Column 3" ); # control hidden to speed up insertion $this->{LISTCTRL}->Show( 0 ); my( $id ); my( $elements ) = 5; foreach ( 1 .. $elements - 1 ) { # WORKS my $nnnn = int(rand(100)); # my $data = int(rand(100)); #my $data2 = int(rand(100)); #my $data3 = int(rand(100)); # DOES NOT WORK my $data = "STRING: " . $nnnn; #my $data = "STRING: " . int(rand(100)); my $data2 = "STRING: " . int(rand(100)); my $data3 = "STRING: " . int(rand(100)); $id = $this->{LISTCTRL}->InsertStringItem( $_, $data ); $this->{LISTCTRL}->SetItemData( $id, $nnnn ); $this->{LISTCTRL}->SetItem( $id, 1, $data2 ); $this->{LISTCTRL}->SetItem( $id, 2, $data3 ); $hash{$nnnn}{0} = $nnnn; $hash{$nnnn}{1} = $data; $hash{$nnnn}{2} = $data2; $hash{$nnnn}{3} = $data3; } $this->{LISTCTRL}->Show( 1 ); my( $item ) = Wx::ListItem->new; $this->{LISTCTRL}->SetColumnWidth( 0, wxLIST_AUTOSIZE ); $this->{LISTCTRL}->SetColumnWidth( 1, wxLIST_AUTOSIZE ); $this->{LISTCTRL}->SetColumnWidth( 2, wxLIST_AUTOSIZE ); my( $topsizer ) = Wx::BoxSizer->new( wxVERTICAL ); $topsizer->Add( $this->{LISTCTRL}, 2, wxGROW ); $topsizer->Add( $this->{LOGWINDOW}, 1, wxGROW ); $this->SetSizer( $topsizer ); $this->SetAutoLayout( 1 ); use Wx::Event qw( EVT_LIST_COL_CLICK ); EVT_LIST_COL_CLICK( $this, $this->{LISTCTRL}, \&OnSort ); $this } sub Cmp { my( $f, $s ) = @_; my( $i1, $i2 ) = ( $hash{$f}{$acol+1}, $hash{$s}{$acol+1} ); print "$f => $s\n"; if ($sort eq 'asc') { if ($i1 lt $i2) { 1; } } elsif ($sort eq 'desc') { if ($i1 gt $i2) { 1; } } } sub OnSort { my( $this, $event ) = @_; $acol = $event->GetColumn; # use Data::Dumper; # print Dumper(%hash); if ($sort eq 'asc') { $sort = 'desc'; } else { $sort = 'asc'; } $this->{LISTCTRL}->SortItems( \&Cmp ); } package MyApp; use strict; use vars qw(@ISA); @ISA = qw(Wx::App); sub OnInit { my( $this ) = @_; my( $frame ) = MyFrame->new( 'wxPerl ListCtrl Test', 50, 50, 450, 34 +0 ); $frame->Show( 1 ); $this->SetTopWindow( $frame ); 1; } package main; my( $app ) = MyApp->new; $app->MainLoop;
    It's a hack, but it works. This ListCtrl stuff has been driving me nuts!!!!!!