in reply to how to manage a large console menu

If you'd like to 'roll your own', how about something like this ...?
my @choices = ( 'system1', 'system2', 'system3', # ... ); #displays the menu sub menu { print "Please pick which system you wish to FTP to:\n"; for (my $i = 0; $i < @choices; $i++) { printf "%3d) %s\n", $i+1, $choices[$i]; } chomp(my $option = <>); }
It would allow you to make changes to your menu choices very easily.

s''(q.S:$/9=(T1';s;(..)(..);$..=substr+crypt($1,$2),2,3;eg;print$..$/

Replies are listed 'Best First'.
Re^2: how to manage a large console menu
by jhourcle (Prior) on May 22, 2006 at 13:45 UTC

    As paulehr made a distinction between 'system1' and 'hostname_to_system1', it might be better to use a hash:

    my %systems = ( system1 => 'hostname_to_system1', system2 => 'hostname_to_system2', ... ); my $choice = menu; dftp( $systems{$choice} ); # sub menu { my @choices = (sort keys %systems) print "Please pick which system you wish to FTP to:\n"; foreach my $i ( 0 .. $#choices ) { printf( "%2i.) %s\n", $i+1, @choices($i)); } # get input ... assuming in $option; if ( $option eq 'q' ) { exit; # or an abort routine to handle cleaning up } elsif ( defined( $choices[$option-1] ) ) { return $choices[$option-1]; } else { print "Unknown option.\nPlease try again\n"; goto &menu; # yes, goto. Please don't give me the 'goto is evil' + speach } }

    (this of course assumes the whole 'roll your own' approach, and not using some of the text-based menu systems available in CPAN)

      Yes, that's a valid point.  I considered using a hash in my example, but decided against it for the reason that, in this particular case, it's pretty easy to make the transformation from 'systemN' to 'hostname_to_systemN' for any value of N.

      The potential downside to making it a hash is that you have to sacrifice keeping it in the order you like (unless order isn't important, or you don't mind just using the order which sort generates).

      Another option which lets you keep the order you prefer, and still have corresponding values for each of your "keys" would be to have both an array *and* a hash, eg.:

      my @systems = qw( this_system_should_be_first system1 system2 system3 the_final_system ); my %systems = ( this_system_should_be_first => 'hostname_to_first_system', system1 => 'hostname_to_system1', system2 => 'hostname_to_system2', system3 => 'hostname_to_system3', the_final_system => 'hostname_to_final_system', );
      which gives a nice flexibility, but requires making updates in two data structures rather than just one.

      The tradeoff one chooses will ultimately be a matter of personal preference and/or need.


      s''(q.S:$/9=(T1';s;(..)(..);$..=substr+crypt($1,$2),2,3;eg;print$..$/
        For slightly less redundancy, how about:
        my @systems = ( [ system1 => host_1 ], [ system2 => host_2 ], [ system3 => host_4 ], #etc );
        my @systems = qw( this_system_should_be_first system1 ... ); my %systems = ( this_system_should_be_first => 'hostname_to_first_system', system1 => 'hostname_to_system1', ... );
        Too much typing there, use hash slice man (after populating "@systems") ...
        my %systems; @{ @systems } = qw( hostname_to_first_system hostname_to_system1 ... ) ;