Ask a Question related to PERL Beginners, Design and Development.

  1. #1

    Default 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

  2. Similar Questions and Discussions

    1. 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...
    2. 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...
    3. 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...
    4. 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...
    5. 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...
  3. #2

    Default Re: Clumsy choice

    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
    John W. Krahn Guest

Posting Permissions

  • You may not post new threads
  • You may post replies
  • You may not post attachments
  • You may not edit your posts

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139