Re: Gtk2::TreeModel - foreach



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]