Re: Gtk2::TreeModel - foreach
- From: "Martin Schlemmer" <Martin Schlemmer nwu ac za>
- To: "gtk-perl-list gnome org" <gtk-perl-list gnome org>, "Martin Schlemmer" <Martin Schlemmer nwu ac za>, "Jeff Hallock" <Jhallock WBandA com>, "Zettai Muri" <zettaimuri ymail com>
- Subject: Re: Gtk2::TreeModel - foreach
- Date: Fri, 20 Nov 2009 13:48:17 +0200
On 2009/11/20 at 11:41 AM, Zettai Muri <zettaimuri ymail com> wrote:
I was just wondering, is there a return value of some sort or a way to
get the return TRUE from the search sub, so that it is possible to then
do something like:
if ( $model->foreach(\&search, 'Bob') )
{
print ("Welcome back!\n");
}
else
{
print ("Who are you?\n");
}
I have tried the above but my output is always:
FOUND
Who are you?
Use pass a hash or other reference to the foreach():
-----
#!/usr/bin/perl -w
use strict;
use warnings;
use Glib qw( TRUE FALSE );
use Gtk2 qw( -init );
my $model = Gtk2::ListStore->new('Glib::String');
foreach my $str ( qw( This is some example test data to populate our list ) ) {
$model->set( $model->append(), 0, $str );
}
&list_find('test');
&list_find('no match');
sub list_find {
my($search) = @_;
my $data = { search => $search, found => FALSE };
$model->foreach( \&listStore_foreach, $data );
if ($data->{found}) {
print "Found: '$search'\n";
}
else {
print "Not found: '$search'\n";
}
}
sub listStore_foreach {
my($model,$path,$iter,$data) = @_;
my($item) = $model->get($iter, 0);
$data->{found} = TRUE if ($item =~ /$data->{search}/);
# return TRUE to end
return FALSE;
};
Vrywaringsklousule / Disclaimer: http://www.nwu.ac.za/it/gov-man/disclaimer.html
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]