Professional Web Applications Themes

Clumsy choice - PERL Beginners

Removed by Administrator...

  1. Moderated Post

    Default Clumsy choice

    Removed by Administrator
    Jan Eden Guest
    Moderated Post

  2. #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

Similar Threads

  1. TCL as CGI.. or PHP and no CGI? The choice is yours
    By Wojciech Kocjan in forum PHP Development
    Replies: 1
    Last Post: August 18th, 07:39 AM
  2. Choice of MTA
    By Haines Brown in forum Debian
    Replies: 2
    Last Post: July 21st, 04:30 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not 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