CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
Search Posts SEARCH
Who's Online WHO'S
Log in LOG

Home: Perl Programming Help: Beginner:
Dynamic menu using perl hash


New User

Jul 23, 2012, 2:08 PM

Post #1 of 3 (1695 views)
Dynamic menu using perl hash Can't Post

I am very new to Perl and I am trying to generate a menu from perl that is dynamically generated from a directory listing. I have the menu displaying correctly but am unable to figure out how to get an element out of the hash based on user inputting the key via the keyboard. Is there a way to do this so that after getting the key from the end-user I return the value?

Below is my code and output:

Server Instance:
size of hash: 15.
1 all
2 default
3 web
4 production
5 standard
6 minimal
7 cimsbri
8 TEST_all
11 capPlanWeb
13 odsWebsvc
14 portalservicesbri
15 custportalbri
Choose Server Instance: 15

You choose option 15

You choose option


$num = 1;

# - Create Hash table
my %hash = ();

# - Store directory listing in an array.
opendir DIR, "/dsto/sw/dev/webapps/jbossEAP5.1/applications/jbossas/server";
my @array = readdir DIR;
closedir DIR;

print "Server Instance:\n";

# - Add array to hash to create the menu
foreach $file(@array) {
next if ($file =~ m/^\./);
$hash{ $num } = $file;
#print "$num\t$file\n";


print "size of hash: " . keys( %hash ) . ".\n";

# - Create the key value pair menu
foreach (sort { $a <=> $b } keys(%hash) )
#print "key: $_ value: $hash{$_}\n"
print "$_ $hash{$_}\n"

# - Get user option
print "Choose Server Instance: ";
$option = <STDIN>;

# Get value from user input key.
print "\nYou choose option $option";
print "\nYou choose option $key{$option}\n";

Any help would be greatly appreciate.


Jul 23, 2012, 8:36 PM

Post #2 of 3 (1683 views)
Re: [cwells3rd] Dynamic menu using perl hash [In reply to] Can't Post

Because your options are always numeric, I would store the menu in an array rather than a hash.

If you know all possible menu values, you can write a subroutine for each one. Build an array of code references to associate the code with the option numbers.

The following code is a small sample of how that works. Of course, you would have to build the arrays dynamically.


use strict;
use warnings;

my @options = (
sub {},
print <<"MENU";
0 exit
1 all
2 default
3 web
print "Option: ";

while ((my $option = <>) != 0) {
print "Option: ";

sub all {
print "executing all\n";

sub default {
print "executing default\n";

sub web {
print "executing web\n";

Good Luck,

New User

Jul 24, 2012, 10:18 AM

Post #3 of 3 (1673 views)
Re: [BillKSmith] Dynamic menu using perl hash [In reply to] Can't Post

Thanks for the reply, that's a very good way to accomplish what I am doing, the problem is that I do not know all the menu values as they are based on the number of folders in a given directory. This directory is expected to grow and I was hoping to write the script in a way that is was smart enough to build the menu based on that number so no additional coding is needed.

The script is only for navigation purposes so people in the company who may not be familiar can quickly get to the directories they need. I may juat do this in Java as both Perl and Python has it's challenges.

I'm taking your array snipit though, it helps with another action item I'm working on Cool


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives