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

Hello all!

I'm new at Perl. I have the results of a search in a Hash, and I need to print the N results in a window using GUI. My doubt is how to embedding a perl repetitive structure (like for) into XUL::Gui (to print the N result on a label).

I post a code of what i want to do (of course that dont work :P)

#! /usr/bin/perl use XUL::Gui; $moreThanOne=5; display Window title => "Pacientes", GroupBox( for($i=0;$i<=$moreThanOne;$i++){ Label (value=>$i); }, );
Thank you all :)

Replies are listed 'Best First'.
Re: XUL::Gui and Loop structure
by lancer (Scribe) on May 31, 2011 at 07:23 UTC

    Try this:

    #!/usr/bin/perl use XUL::Gui; $moreThanOne=5; sub build_list { my $howmany = shift; my @list = (); for(my $i = 0, $imax = $howmany; $i <= $imax; $i++) { push @list, Label (value=>$i); } return @list; } @pacientes = build_list $moreThanOne; display Window title => "Pacientes", width => 640, height => 480, Grou +pBox (@pacientes);
      Thank you!! :) This was just I need =)
        Your welcome! I'm glad it was helpful! :)
      Why the C-style for loop? More Perlish to use the '..' operator.
      #!/usr/bin/perl use XUL::Gui; $moreThanOne=5; sub build_list { my $howmany = shift; my @list = (); push @list, Label(value=>$_) for 0 .. $howmany; return @list; } @pacientes = build_list $moreThanOne; display Window title => "Pacientes", width => 640, height => 480, Grou +pBox (@pacientes);
      UPDATE: The original post was edited. The code in this thread (one started by lancer) is based on the original code.

      Elda Taluta; Sarks Sark; Ark Arks

        If you want to be more Perlish, drop the @list.

        sub build_list { my $max = shift(); return map Label(value=>$_), 0..$max; }

        The name $howmany was misleading, you were generating more items than that as you were stargin with zero. And now there's little reason to keep on using a subroutine.

        Jenda
        Enoch was right!
        Enjoy the last years of Rome.

Re: XUL::Gui and Loop structure
by deep3101 (Acolyte) on May 31, 2011 at 03:07 UTC

    Try storing the search results in a scalar or array and display it using Label.

    Something like:
    my %hash(...define your hash here...); my $searchterm=<>; my $store; #or my @store if(exists($hash{$searchterm})) { $store=$hash{$searchterm}; #or you can use push(@store,$hash{$searchterm}) } #some code #to print on label just pass the $store or @store as #parameter to Label() of your GUI package.

      And repeat that as many times you want within a for(;;) while() or until() etc.