Re: Gtk2::TreeModel - foreach





If you supply $user_data - then you it will be passed in as the last argument to 
the ForEach function.



$model->foreach(
    sub {
        my($model,$path,$iter,$user_data <-------------) = @_;
        my($item)              = $model->get($iter, 0);

        print "$item\n";

        # return TRUE to end
        return FALSE;
    }, $user_data <----------------------- ???
);


Here is an example that searches for a name in model:

sub search {
        my($model,$path,$iter,$search_string) = @_;
      my($item) = $model->get($iter, 0);

        if ($item =~ /$search_string/) {
          print "FOUND\n";
          return TRUE;
      }
      else {
          return FALSE;
      }
}

$model->foreach(\&search, 'Bob');
$model->foreach(\&search, 'Billy');


Thank you!

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?

I was going to add after the print "FOUND\n"; line something like:
$my_global_var = 'FOUND';

and then use that to test against, and it's only for me to use in my
own program but global variables are supposed to be avoided aren't they?

Thanks again for any input.


      __________________________________________________________________________________
Win 1 of 4 Sony home entertainment packs thanks to Yahoo!7.
Enter now: http://au.docs.yahoo.com/homepageset/




[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]