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/ ___ gtk-perl-list mailing list gtk-perl-list@gnome.org http://mail.gnome.org/mailman/listinfo/gtk-perl-list
RE: Gtk2::TreeModel - foreach
-Original Message- From: Martin Schlemmer [mailto:martin.schlem...@nwu.ac.za] Sent: Friday, November 20, 2009 6:48 AM To: gtk-perl-list@gnome.org; Martin Schlemmer; Jeff Hallock; Zettai Muri Subject: Re: Gtk2::TreeModel - foreach On 2009/11/20 at 11:41 AM, Zettai Muri zettaim...@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(): Or here is an alternative that doesn't use the foreach method. sub search { my ($model, $search_string) = @_; my $iter = $model-iter_first; my $found; do { if ($model-get($iter, 0) ~= /$search_string/) { $found = 1; } else { $iter = $model-iter_next; } } while (! $found $iter); return $found; } if ( search($model, 'bob') ) { print (Welcome back!\n); } else { print (Who are you!\n); } ___ gtk-perl-list mailing list gtk-perl-list@gnome.org http://mail.gnome.org/mailman/listinfo/gtk-perl-list
Re: Gtk2::TreeModel - foreach
Use pass a hash or other reference to the foreach(): Martin, very cool! I sure have a long way to go before I can even imagine a solution like this. Or here is an alternative that doesn't use the foreach method. sub search { my ($model, $search_string) = @_; my $iter = $model-iter_first; my $found; do { if ($model-get($iter, 0) ~= /$search_string/) { $found = 1; } else { $iter = $model-iter_next; } } while (! $found $iter); return $found; } if ( search($model, 'bob') ) { print (Welcome back!\n); } else { print (Who are you!\n); } Jeff, thank you for keeping it simple and offering an alternative. You guys make it look so easy. ZM. __ Win 1 of 4 Sony home entertainment packs thanks to Yahoo!7. Enter now: http://au.docs.yahoo.com/homepageset/ ___ gtk-perl-list mailing list gtk-perl-list@gnome.org http://mail.gnome.org/mailman/listinfo/gtk-perl-list
Re: Gtk2::TreeModel - foreach
On 2009/11/19 at 01:44 PM, Zettai Muri zettaim...@ymail.com wrote: Hi, Would someone be able to point me towards a good tutorial/simple example on how to use the following method? $model-foreach ($func, $user_data=undef) * $func (subroutine) * $user_data (scalar) Call $func on each row in $model. $func gets the tree path and iter of the current row; if $func returns true, the tree ceases to be walked, and $treemodel-foreach returns. I have been googling it and turning up heaps of references to the doco and something in the archives titled: Gtk2::TreeModel::foreach callback not getting any arguments? but was hoping to get something simpler. Not exactly sure what you want, but attached is an example for a Gtk2::ListStore's foreach. Regards, Martin Vrywaringsklousule / Disclaimer: http://www.nwu.ac.za/it/gov-man/disclaimer.html model_foreach_test.pl Description: Binary data ___ gtk-perl-list mailing list gtk-perl-list@gnome.org http://mail.gnome.org/mailman/listinfo/gtk-perl-list
Re: Gtk2::TreeModel - foreach
Not exactly sure what you want, but attached is an example for a Gtk2::ListStore's foreach. Thanks Martin, that was perfect. Nice and easy even I can understand. Regards, Martin Vrywaringsklousule / Disclaimer: http://www.nwu.ac.za/it/gov-man/disclaimer.html __ Win 1 of 4 Sony home entertainment packs thanks to Yahoo!7. Enter now: http://au.docs.yahoo.com/homepageset/ ___ gtk-perl-list mailing list gtk-perl-list@gnome.org http://mail.gnome.org/mailman/listinfo/gtk-perl-list
Re: Gtk2::TreeModel - foreach
Not exactly sure what you want, but attached is an example for a Gtk2::ListStore's foreach. Where does the $user_data (scalar) part of the method come into play? Is it possible to get an extended example that shows how this is used? $model-foreach( sub { my($model,$path,$iter) = @_; my($item) = $model-get($iter, 0); print $item\n; # return TRUE to end return FALSE; }, $user_data --- ??? ); Is this data within the ListStore or some other data passed to the method? How does the method act on this data? Again thanks for the help. __ Win 1 of 4 Sony home entertainment packs thanks to Yahoo!7. Enter now: http://au.docs.yahoo.com/homepageset/ ___ gtk-perl-list mailing list gtk-perl-list@gnome.org http://mail.gnome.org/mailman/listinfo/gtk-perl-list
RE: Gtk2::TreeModel - foreach
-Original Message- From: gtk-perl-list-boun...@gnome.org [mailto:gtk-perl-list-boun...@gnome.org] On Behalf Of Zettai Muri Sent: Thursday, November 19, 2009 3:56 PM To: Martin Schlemmer; gtk-perl-list@gnome.org Subject: Re: Gtk2::TreeModel - foreach Not exactly sure what you want, but attached is an example for a Gtk2::ListStore's foreach. Where does the $user_data (scalar) part of the method come into play? Is it possible to get an extended example that shows how this is used? $model-foreach( sub { my($model,$path,$iter) = @_; my($item) = $model-get($iter, 0); print $item\n; # return TRUE to end return FALSE; }, $user_data --- ??? ); Is this data within the ListStore or some other data passed to the method? How does the method act on this data? Again thanks for the help. 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'); ___ gtk-perl-list mailing list gtk-perl-list@gnome.org http://mail.gnome.org/mailman/listinfo/gtk-perl-list
Re: Gtk2::TreeModel - foreach
On Nov 19, 2009, at 4:15 PM, Jeff Hallock wrote: -Original Message- From: gtk-perl-list-boun...@gnome.org [mailto:gtk-perl-list- boun...@gnome.org] On Behalf Of Zettai Muri Sent: Thursday, November 19, 2009 3:56 PM To: Martin Schlemmer; gtk-perl-list@gnome.org Subject: Re: Gtk2::TreeModel - foreach Where does the $user_data (scalar) part of the method come into play? Is it possible to get an extended example that shows how this is used? Is this data within the ListStore or some other data passed to the method? How does the method act on this data? Again thanks for the help. If you supply $user_data - then you it will be passed in as the last argument to the ForEach function. If you're thinking, but i can just use a closure for that, then you're already thinking perlishly. The C api has user data pointers all over the place, because C doesn't have closures. In perl, you get the option of using a closure or passing different parameters to a single function. You'd choose to use the single function plus user data instead of a closure in a situation in which the closure would introduce a reference cycle. Pretty much any other time, you'd use a closure. -- I can't believe i'm having to arbitrate who gets to have what dress on whose monkey. -- Me, exasperated at the ridiculousness of parenthood. ___ gtk-perl-list mailing list gtk-perl-list@gnome.org http://mail.gnome.org/mailman/listinfo/gtk-perl-list