Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

Hash vs. Scalar+Text vs. DBD

by walkingthecow (Friar)
on Sep 24, 2009 at 21:39 UTC ( [id://797332]=perlquestion: print w/replies, xml ) Need Help??

walkingthecow has asked for the wisdom of the Perl Monks concerning the following question:

Hello monks,

I have a simple command line menu when my script starts up. The menu is quite easy, but I am trying to figure out the best way to store the data. Here's an example (in the actual code there are 15 items per server):

my ($server,$db_type,$server_location,$server_function); my @servers; while (1) { system("clear"); print "\t\t\tPLEASE CHOOSE AN OPTION FROM BELOW\n"; print <<EOF; (1) Server Name: $server (2) Database Type: $db_type (3) Server Location: $server_location (4) Server Function: $server_function ------------------------------------------- (A) Add Another Server (C) Continue (D) Done * - item is optional. EOF print "Enter the number you would like to edit > "; chomp( my $choice = <STDIN> ); if ( $choice == 1 ) { print "Please enter the server name: "; chomp( $server = <STDIN> ); } elsif ( $choice == 2 ) { print "Please enter the database type: "; chomp( $db_type = <STDIN> ); } elsif ( $choice == 3 ) { print "Please enter the server location: "; chomp( $server_location = <STDIN> ); } elsif ( $choice == 4 ) { print "Please enter the server function: "; chomp( $server_function = <STDIN> ); } elsif ( $choice =~ /a/i ) { push(@servers,"$server,$db_type,$server_location,$server_f +unction); $server=""; $db_type=""; $server_location=""; $server_function=""; } elsif ( $choice =~ /c/i ) { push(@servers,"$server,$db_type,$server_location,$server_f +unction); last; } else { next; } } ..... # later in the code for my $server (@servers) { my ($server,$db_type,$server...)=split(',',$server); do stuff; }


My question:
Is there a better way that this can be done, so that later the server data can be looked up by hostname or ip? Is there a better way to do the menu? This way just seems terrible.

Replies are listed 'Best First'.
Re: Hash vs. Scalar+Text vs. DBD
by GrandFather (Saint) on Sep 25, 2009 at 01:10 UTC

    Yes, yes and yes. Consider:

    use strict; use warnings; my @servers; my %dataOptions = ( 1 => ['server', 'Server Name:', 1], 2 => ['dbType', 'Database Type:', 0], 3 => ['location', 'Server Location:', 0], 4 => ['function', 'Server Function:', 0], 5 => ['ip', 'Server IP:', 1], ); my %flowOptions = ( A => 'Add Another Server', C => 'Continue', D => 'Done', ); my %byType; addServer (\@servers); while (1) { #system ("clear"); printf " (%s) %s%-20s %s\n", $_, $dataOptions{$_}[2] ? '*' : ' ', @{$dataOptions{$_}}[1], $servers[-1]{$dataOptions{$_}[0]}, for sort keys %dataOptions; print '-' x 30, "\n"; printf " (%s) %s\n", $_, $flowOptions{$_} for sort keys %flowOpti +ons; print "Enter the number you would like to edit > "; chomp (my $choice = uc <STDIN>); if (exists $dataOptions{$choice}) { print "Please enter the ", $dataOptions{$choice}[1], ' '; chomp ($servers[-1]{$dataOptions{$choice}[0]} = <STDIN>); $byType{$dataOptions{$choice}[0]}{$#servers} = 1; } elsif (! exists $flowOptions{$choice}) { next; } last if $choice eq 'D'; # Can't add another unless all required data supplied for current +entry next if missingOptions (\%dataOptions, $servers[-1]); addServer (\@servers); } # Remove bogus last entry pop @servers if missingOptions (\%dataOptions, $servers[-1]); # later in the code for my $server (@servers){ print "Server $server->{server}\n"; } for my $serverIndex (sort keys %{$byType{ip}}) { print "Server $servers[$serverIndex]{server}: $servers[$serverInde +x]{ip}\n"; } sub addServer { my ($servers) = @_; push @$servers, {}; # Add a new entry $servers->[-1]{$dataOptions{$_}[0]} = '' for keys %dataOptions; } sub missingOptions { my ($options, $data) = @_; my @missing = grep {$options->{$_}[2] && ! length $data->{$options->{$_}[0]} +} keys %$options; return @missing; }

    True laziness is hard work

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://797332]
Approved by GrandFather
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (4)
As of 2024-03-29 08:09 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found