Ask a Question related to PERL Beginners, Design and Development.
-
Jan Eden #1
Clumsy choice
Hi,
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?
Thanks,
Jan
--
The day Microsoft makes something that doesn't suck is the day they start selling vacuum cleaners.
Jan Eden Guest
-
is director the right choice?
Hi all. We plan to produce interactive presentations for our sales (which they also can give away on CD to customers) and throw away the old... -
language choice in RH 9
I am trying to understand how language choice in RH 9 works, in particular in relationship to the locale database. The reason is that some of my... -
TCL as CGI.. or PHP and no CGI? The choice is yours
Phil Powell wrote: That's actually a bit of a tough one. This is a very common permission problem, since your scripts run as www, and not as... -
Choice of MTA
I don't want to start a flame war, but need some input in order to choose a mail transport agent. The ones of interest to me are primarily sendmail... -
What's the best choice?
I don't think that the Question is Asp Vs XML, but rather Asp with or without XML. Personally, XML seems to me to only add undue complexity when... -
John W. Krahn #2
Re: Clumsy choice
Jan Eden wrote:
Hello,>
> Hi,
> 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
John W. Krahn Guest



Reply With Quote

