"John W. Krahn" <jwkr...@shaw.ca> writes:
[...] > Why are you using push and sprintf? > > my @relist = "$r1name\n----- -----\n"; Much better... this is the only place I where I have anything that could even remotely be an excuse.... I tried using @relist, print "blah\n" Which didn't work then went to sprintf instead of seeing the easier solution. [...] > Does $chosen really need to be visible at this scope? Probably not. So you mean to use `my $chosen' where ever I can? [...] > You can't use last to exit a subroutine, only to exit a loop. OK, but here is the problem then. Using `next' which is also not supposed to be use there or `return' which is, does not cause the iteration required to continue. How can I make those sub functions cause an iteration and new supply of names to work on? >> L => sub { print relist(@relist); }, > > Why are you printing the return value of relist() which is undef? Why > not just: > L => sub { print @relist, "Relisting only, no action > taken\n"; }, No reason... other than dim wittedness. Tried to get rid of some of the worst of the bad stuff below but how to make iteration happen at A and N? L and q are the only choices that shouldn't cause iteration One way I thought of is to just go with `if/elsif/else' and ditch the hash table. `if/elsif/else' seems to be more versatile. Or at least I can get this done with that kind of construct. And it seems kind of lame to keep the hash table just for L. (relist the choices) since its the only one really that doesn't need to iterate. Another creeping change is that it seems like it would work faster to allow the user to input both a file number and an action in one move, if something besides a default action is required. So tried that here too. ------- --------- ---=--- --------- -------- #!/usr/local/bin/perl use strict; use warnings; ## These names ARE meaningless. could as well be foo, bar etc ## They represent filanems from specific directory (r2) my @ar1 =qw( r2one r2two r2three r2four r2five r2six r2seven r2eight r2nine r2ten r2eleven r2twelve ); ## This name represents a file from a different directory. my $r1name = "r1fname"; for ( my $i = 0; $i <= $#ar1; $i += 3 ) { dispt( $r1name, @ar1[ $i .. $i + 2 ] ); } ## dispatch table to handle file names sub dispt { ## This element will never be acted on. It is for display ## and comparison only my ($r1name, @h ) = @_; print "$r1name\n----- -----\n"; ## creating an array containing this printed section ## to be used to redisplay current list. push my @relist, "$r1name\n----- -----\n"; for my $key ( 0 .. $#h ) { printf "%2d %s\n", $key + 1, $h[ $key ]; push @relist, sprintf "%2d %s\n", $key + 1, $h[ $key ]; } print "----- -----\n"; push @relist, "----- -----\n"; my $exit_str = "\nAborted by user..\n"; my $con_str = "\nContinuing... no action taken\n"; while ( 1 ) { print <<'PROMPT'; N (a number) take default action on file number `N' Na (a number + letter `a') take action_a on file number `N' l to redisplay the list no action taken on any files c to continue no action taken on any files q to Exit abort completely PROMPT ## Take in users choice chomp(my $chosen = <STDIN>); # quit if ( $chosen eq 'q' ) { print $exit_str; exit 0; } # continue elsif ( $chosen eq 'c' ) { print $con_str; last; } # relist elsif ($chosen eq "l") { print "@relist\n"; } # Default action (matching a number by itself) elsif ( $chosen =~ /\A\d+\z/ && $chosen >= 1 && $chosen <= @h ) { print "Taking some default action on <$h[$chosen - 1]>\n"; last; } # action N (matching a number letter combination) elsif ( $chosen =~ /\A(\d+)a\z/) { my ($num) = $chosen =~ m/(\d+)/; if ($num >= 1 && $num <= @h) { print "Taking action `a' on <$h[$num - 1]>\n"; last; } } ## [...] more choices here else { print "\n*** <$chosen> ***\nis an INVALID choice, please try again,\n", "or press L to relist choices and try again\n\n"; } } } -- To unsubscribe, e-mail: beginners-unsubscr...@perl.org For additional commands, e-mail: beginners-h...@perl.org http://learn.perl.org/