Jan Eden wrote:
> 
> Hi,

Hello,

> I wrote a script to update some websites based on a template system. The
> script starts asking the user to choose a site to update:
> 
> [script preamble omitted]
> 
> my ($update_path, $site_root, $site_type);
> 
> my %choice_hash = (
>     1   => 'janeden',
>     2   => 'gargnano',
>     3   => 'breskens',
> );
> 
> my %site_hash = (
>     janeden => "/Users/jan/Sites/jan-eden",
>     gargnano    => "/Users/jan/Sites/gargnano",
>     breskens    => "/Users/jan/Sites/breskens",
> );
> 
> print "\nPlease choose a site!\n\n";
> 
> foreach (sort {$a <=> $b} keys %choice_hash) { printf "(%s) %15s\n", $_, 
> $choice_hash{$_}; }
> 
> print "Choice: ";
> 
> chomp (my $site_choice = <STDIN>);
> 
> if (exists $choice_hash{$site_choice}) {
>     $site_type = $choice_hash{$site_choice};
>     $site_root = $site_hash{$site_type}
> }
> else {
>     die "No valid site - scripts exits.\n";
> }
> 
> While this works, it seems a little clumsy to use two hashes for the
> choice/sitename and sitename/siteroot. I read about hashes of hashes
> (using references). Could I use this method here to simplify my script?


You could probably use an array of arrays


my @sites = (
    [ janeden  => '/Users/jan/Sites/jan-eden' ],
    [ gargnano => '/Users/jan/Sites/gargnano' ],
    [ breskens => '/Users/jan/Sites/breskens' ],
    );

print "\nPlease choose a site!\n\n";
for ( 1 .. @sites ) {
    printf "(%d) %s\n", $_, $sites[ $_ - 1 ][ 0 ];
    }

print 'Choice: ';

my $site_choice = <STDIN> - 1;

my ( $site_type, $site_root );
if ( exists $sites[ $site_choice ] ) {
    ( $site_type, $site_root ) = @{ $sites[ $site_choice ] };
    }
else {
    die "No valid site - scripts exits.\n";
    }



John
-- 
use Perl;
program
fulfillment

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>


Reply via email to