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

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

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

 



cwells3rd
New User

Jul 23, 2012, 2:08 PM

Post #1 of 3 (1295 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:

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

You choose option 15

You choose option


Code:
#!/usr/bin/perl

$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;

system('clear');
print "Server Instance:\n";

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

}

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.


BillKSmith
Veteran

Jul 23, 2012, 8:36 PM

Post #2 of 3 (1283 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.


Code
  

use strict;
use warnings;

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




while ((my $option = <>) != 0) {
$options[$option]->();
print "Option: ";
}

sub all {
print "executing all\n";
}

sub default {
print "executing default\n";
}

sub web {
print "executing web\n";
}

Good Luck,
Bill


cwells3rd
New User

Jul 24, 2012, 10:18 AM

Post #3 of 3 (1273 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