Loading ...
Sorry, an error occurred while loading the content.
 

Re: foreach'ing a call to a subroutine

Expand Messages
  • Steve Milo
    I have one problem though, I generate a dynamic choice of options on the menu. What happens is I pull out some information from another file and generate the
    Message 1 of 4 , Nov 30, 2000
      I have one problem though, I generate a dynamic choice of options on
      the menu. What happens is I pull out some information from another
      file and generate the menu based on that. Ex:

      /file/that/contains/info:
      junk="a b c d";

      This can change from day to day.

      So what I do is something like this:

      @menu ("One whole choice", "Another whole choice", "one more here");

      Open (FILE, "/file/that/contains/info");
      chomp (@file....);
      close;
      foreach $file(@file) {
      if ($file =~ /^junk=\(grabbing the letters here)/) {
      $junk = split....;
      foreach $o($junk) {
      @menu=( @menu, "choices $o" ); }

      @menu= ( "choices 1", "choices 2");
      } #End of foreach

      foreach ($o=0; $o < @menu; $o++) {
      printf ....so on and so forth;
      } #End of foreach



      How would I get the look up to be generated dynamically along with
      the
      menu I create?

      Steve M

      --- In perl-beginner@egroups.com, Andrew Johnson <
      andrew-johnson@h...>
      wrote:
      > ! What I'm trying to accomplish is that I put all the names of
      these
      > ! subroutines that I'm calling into an array. If the user makes
      the
      > ! choice in the menu that requires calling all the subroutines.
      This
      > ! particular subroutine has to call all the others.
      >
      > I'm not sure I understand your question exactly, but I have
      > some thoughts that might help you out.
      >
      > You could simplify your task by creating a dispatch table of the
      > routines you want to call -- make a hash of references to the
      > subroutines, and create a menu array using the keys of the hash as
      > values.
      >
      > By doing this, you can avoid all the if/elsif/... testing and
      replace
      > it with a simple hash look-up.
      >
      > As for one subroutine calling other routines, simply call the other
      > routines from the body of that particular routine.
      >
      > Here is a simple example that presents a menu, gets the user
      > selection (checks validity) and then calls the appropriate routine:
      >
      > #!/usr/bin/perl -w
      > use strict;
      >
      > my %subs = ( first => \&sub_a,
      > second => \&sub_b,
      > third => \&sub_c,
      > all => \&sub_d,
      > );
      >
      > my @menu = qw(first second third all);
      >
      > # let's show the user the menu and get the input:
      >
      > print "Please select a number from the menu:\n";
      > foreach my $i (0 .. $#menu) {
      > print "\t$i: $menu[$i]\n";
      > }
      > print "? ";
      > my $selection = <STDIN>;
      > chomp $selection;
      >
      > # now let's check validity (only digits means it is a positive
      > # integer, and we also check that it isn't too high):
      >
      > $selection =~ /^(\d+)$/ and $1 < @menu or die "Invalid
      selection\n";
      >
      > # now we merely call the subroutine in question by using the
      > # entry in the @menu array as the key to the %subs hash and
      > # use the arrow dereference syntax to call the subroutine:
      >
      > $subs{$menu[$selection]}->();
      >
      > # here are the subs:
      >
      > sub sub_a {
      > print "You chose 'first'\n";
      > }
      >
      > sub sub_b {
      > print "You chose 'second'\n";
      > }
      >
      > sub sub_c {
      > print "You chose 'third'\n";
      > }
      >
      > sub sub_d {
      > print "You chose 'all': which means ...\n";
      > sub_a();
      > sub_b();
      > sub_c();
      > }
      > __END__
      >
      > The above assumes you have at least version 5 of Perl, and it needs
      > Perl version 5.004xx or later to use the arrow deref syntax on
      > subroutine references -- you could also just use the ampersand
      > subroutine deref syntax which would look like:
      >
      > &{ $subs{$menu[$selection]} };
      >
      > I hope this of some assistance for your question.
      >
      > regards,
      > andrew
      >
      > --
      > Andrew L. Johnson http://members.home.net/andrew-johnson/
    Your message has been successfully submitted and would be delivered to recipients shortly.