Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

for loops

by Anonymous Monk
on Apr 10, 2006 at 16:54 UTC ( [id://542321]=perlquestion: print w/replies, xml ) Need Help??

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

I have the below 12 outer ball shapes forming a ring shape, and I can only bind a mouse click to the $refitem one(one of the balls in the ring of balls as it were)... can anyone show me how to bind to one of the balls created by the for loop?
# # bit of code # # outer marker ball one my $refitem = $zinc->add('arc',$centergroup2, [ [10,20], [20,30] ], -filled => 1, -fillcolor => $refgrad, ## $refgrad -linewidth => 0, -priority => 100 ); # make/clone 11/12 more of the above... and form the ring shape for (1..11){ my $relement = $zinc->clone($refitem); $zinc->rotate($relement,.53*$_); } $zinc->bind($refitem, '<1>', \&ro6); # this is the one that w +orks when I click on it #$zinc->bind($relement[1], '<1>', \&ro6b); # Looking for this part
Don't know the right wording as I'm not a programmer... put this makes abit of sence to me: If I could make an array... made up of each of the 11/12 balls created in the for loop's names, then the "#$zinc->bind($varname1, '<1>', \&ro6b);" bit as it were, might work. # Am on Windows XP Home # With ActiveState ActivePerl 5.8.7 Build 815 # Also use EnginSite Perl Editor LITE

Replies are listed 'Best First'.
Re: for loops
by samtregar (Abbot) on Apr 10, 2006 at 17:01 UTC
    You're real close. All you need now is an array to store the cloned balls. Put a line like this above the for loop:

       my @cloned_balls;

    Then a line inside your loop to put the balls in the array. For example, using push():

       push(@cloned_balls, $relement);

    Now you should be able bind them using the array of balls:

       $zinc->bind($cloned_balls[0], '<1>',  \&ro6b);

    -sam

      Thanks very much samtregar... I'll give it a go in the morrow... Sound perfect though... me likey :)
Re: for loops
by zentara (Archbishop) on Apr 10, 2006 at 19:38 UTC
    I'm not sure what types of binding you want to make, do you want to bind all of them to <1>?

    I think you are overlooking "tags", but there are a few ways to do it. The array method is ok, but you would be better off using a hash. There are many different ways to do it, depending on whether you need separate callbacks for each ball or not.

    untested( but close to correct) :-)

    # first way with tags, all outer balls will be bound # to same callback my $refitem = $zinc->add('arc',$centergroup2, [ [10,20], [20,30] ], -filled => 1, -fillcolor => $refgrad, ## $refgrad -linewidth => 0, -priority => 100 -tags => ['outerring'], ); # make/clone 11/12 more of the above for (1..11){ my $relement = $zinc->clone($refitem); $zinc->rotate($relement,.53*$_); } $zinc->bind( 'outerring', '<1>', \&ro6);
    Now you can also do it to bind each outerring ball to a separate callback.
    my %refitems; my $refitem = $zinc->add('arc',$centergroup2, [ [10,20], [20,30] ], -filled => 1, -fillcolor => $refgrad, ## $refgrad -linewidth => 0, -priority => 100 ); # make/clone 11/12 more of the above for (1..11){ $refitem{$_} = $zinc->clone($refitem); $zinc->rotate($refitem{$_},.53*$_); #here you bind each one separately, and/or #pass extra info to the callback $zinc->bind( $refitem{$_}, '<1>', [ \&ro6, $_ ] ) ; }
    As you can see, there are more than one way to do it. You can even assign the hash element as a tag, although it is a bit redundant; however it allows you to search for items thru the tag mechanism. I include it just to reinforce the idea that you can throw anything into the item's tags.
    my $refitem = $zinc->add('arc',$centergroup2, [ [10,20], [20,30] ], -filled => 1, -fillcolor => $refgrad, ## $refgrad -linewidth => 0, -priority => 100 -tags => ['outerring'], ); my %refitems; # make/clone 11/12 more of the above for (1..11){ $refitem{$_} = $zinc->clone($refitem); $zinc->addtag($refitem{$_}, $refitem{$_}); $zinc->rotate($refitem{$_},.53*$_); #here you bind each one separately, and/or #pass extra info to the callback $zinc->bind( $refitem{$_}, '<1>', [ \&ro6, $_ ] ) ; }

    Anyways, those are some ideas to explore. When you have more than 1 item, that you want to bind to the same callback, you can use tags, or pass an extra arg to the bind callback, to identify which ball called it.


    I'm not really a human, but I play one on earth. flash japh
      Thanks again zentara... I when over samtregar's reply today and it worked for me... and I kind of understood it 2... I'll have to take some time on your reply though... Thanks for your tutorals(did work through them, and they were very useful) :)

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://542321]
Approved by wfsp
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others sharing their wisdom with the Monastery: (3)
As of 2024-04-25 09:27 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found