Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

I've been struggling with this for almost a day now and can't seem to find a fix. I have a wxwidget app that I need determine which word was clicked on. I'm able to do this with the exception of the first word after the first row. It always gets the suffix but the prefix is truncated depending on where I click in the word. I've posted my example code below.
#!c:/perl/bin/perl.exe use strict; use warnings; use Wx qw[:everything]; package main; unless(caller){ local *Wx::App::OnInit = sub{1}; my $app = Wx::App->new(); Wx::InitAllImageHandlers(); my $ui = Test_ui->new(); $app->SetTopWindow($ui); $ui->Show(1); $app->MainLoop(); } package Test_ui; use Wx qw[:everything :allclasses]; use Wx::Event qw(EVT_LEFT_UP); our $mainframe; our @keywords=(); use base qw(Wx::Frame); sub new { my( $self, $parent, $id, $title, $pos, $size, $style, $name ) += @_; $parent = undef unless defined $parent; $id = -1 unless defined $id; $title = "" unless defined $title; $pos = wxDefaultPosition unless defined $pos; $size = wxDefaultSize unless defined $size; $name = "" unless defined $name; $style = wxDEFAULT_FRAME_STYLE unless defined $style; $self = $self->SUPER::new( $parent, $id, $title, $pos, $size, $style, $name ); $mainframe=$self; my $main_sizer=Wx::BoxSizer->new(wxVERTICAL); $self->{keywords_text} = Wx::TextCtrl->new($self, -1, "This is a string that contains wrods and other goodie +s for my example. My problem is extracting the first word with multi +ple lines", wxDefaultPosition, wxDefaultSize, wxTE_READONLY|wxTE_MULTILINE|wxTE_WORDWRAP); $main_sizer->Add( $self->{keywords_text}, 1, wxEXPAND|wxALL, 2 +); $self->SetAutoLayout(1); $self->SetSizer($main_sizer); $self->Layout(); EVT_LEFT_UP( $self->{keywords_text}, sub { my ($self, $event) = @_; my $ip=$self->GetInsertionPoint(); my ($col,$row)=$self->PositionToXY($ip); print "row: $row\n"; print "col: $col\n"; $self->ReleaseMouse(); my $clickedline=$self->GetLineText($row); #print "clickedline : $clickedline\n"; my ($prefix) = ""; if ( $row > 0 ) { my $new_col = ($col > 0) ? ($col - $row) : $col; ($prefix) = substr($clickedline,$row,$new_col) =~ /([(\/|\ +:|\.|\n)?\w+]+)$/; } else { ($prefix) = substr($clickedline,$row,$col) =~ /([(\/|\:|\. +|\@)?\w+]+)$/; } my ($suffix)= substr($clickedline,$col,1000) =~ /^([(\/|\:|\.) +?\w+'`]+)/; my $clickedword=$prefix.$suffix; print "Prefix: $prefix\n"; print "suffix: $suffix\n"; print "clicked word: $clickedword\n"; }); return $self; } 1;
Any ideas? am I calculating the substring wrong or is there a hidden character in the wordwrap string that I'm missing?

Any help would be greatly appreciated.

Replies are listed 'Best First'.
Re: substring and wordwrap issue
by Anonymous Monk on Sep 20, 2009 at 20:30 UTC
    am I calculating the substring wrong or is there a hidden character in the wordwrap string that I'm missing?

    Yes. For some reason you are using $row but you should ignore $row.

    $prefix = $1 if substr($clickedline, 0, $col) =~ /(\w+)\z/; $suffix = $1 if $clickedline =~ m/^.{$col}(\w+)/;
      ahh...that did the trick and is much cleaner then my example :)

      Thanks!