Erwin Krause <> wrote:
> Hello,
> 
> I'm new on this list and perl and hope you can help me out with a
> little problem. 
> 
> I have a little problem with a hash / key reference.
> I have two hashes, where 1 hash stores a configuration and the other
> one stores the description for some keys / values of %h1 
> 
> my %config = {
>    'ip' => 'value1',
>    'netm' => 'value2',
>    'bcast' => 'value3',
> }
> 
> my %configdesc = {
>    'ip' => "this is value1 !",
>    'netm' => "this is value1 !",
> }

# This specifies the order in which config is entered.
my @config_ask_order = qw{ip netm bcast};

> 
> 
> 
> than in my code i ask the user for the values I have a key in %h2.
> sub ask_config {

A "local $| = 1;" might help here. See 'perldoc perlvar' for what it
does.

>    foreach (keys %configdesc) {

foreach my $key (@config_ask_order) {

Also, replace $_ by $key in the following. I think it is clearer, and it
makes the explicit assignment "my $key = $_;" redundant.


>        if ($config{$_}) {
>            printf gettext "Please enter %s", $configdesc{$_};
>            print "\n";

Interpolation can replace that printf, and is often simpler, so...

print "Please enter $configdesc{$key}\n"'

>                      # print default value for array and scalars
>            if (ref($config{$_}) eq "ARRAY") {
>                   printf gettext "(default: %s) : ", join(' ',
@{$config{$_}});

The interpolation of arrays does much the same thing (by default,
anyway). So ...

print "(default: (@{$config{$key}}) : ";

(What is gettext?)

>            } else {
>                printf gettext "(default: %s) : ", join(' ',
>            $config{$_}); }

print "(default ($config{$key}): ";

>            my $input = <STDIN>; chomp($input);
> 
>            # array handling
>            if (ref($config{$_}) eq "ARRAY") {
>                my $key = $_;
>                if ($input ne '') {
>                    my @tmp = split(/ /, $input);
>                    $config{$key} = @tmp;

That should be "$config{$key} = [EMAIL PROTECTED];" or "$config{$key} = [EMAIL 
PROTECTED];"

>                }
>            } else {
>                $config{$_} = $input if ($input ne '');
>            }
>        }
>    }
> }
> 
> 
> 
> So now my problem is, that i have to ask the user in a specific order.
> (ip before netmask, a.s.o.)
> I thought i simply can change things like that: (i tested both ways,
> 1_ip and 2_netm) 
> 
> my %config = {
>    'ip' => 'value1',
>    'netm' => 'value2',
>    'bcast' => 'value3',
>    '1_ip' => \$config{'ip'},
>    '2_netm' => sub { return $config{'ip'} }
>    '2_netm' => \&get_hostname,
> }
> 
> my %configdesc = {
>    '1_ip' => "this is value1 !",
>    '2_netm' => "this is value1 !",
> }
> 
> with:
> sub get_hostname {
>    return ${$config{'hostname'}};
> }

I find it hard to see how that helps with what you are trying to do.

> 
> 
> 
> both require to disable subs or refs in strict what is not the best
> way, i know. 
> 
> Some1 have an idea to fix this problem or maybe do the whole thing
> some better? 
> I dont wanna rewrite my whole code to change the config{ip} to
> config{1_ip}. 

For an alternative, investigate Tie::IxHash
(http://search.cpan.org/~gsar/Tie-IxHash-1.21/lib/Tie/IxHash.pm).

HTH

-- 
Brian Raven 

=========================================
Atos Euronext Market Solutions Disclaimer
=========================================

The information contained in this e-mail is confidential and solely for the 
intended addressee(s). Unauthorised reproduction, disclosure, modification, 
and/or distribution of this email may be unlawful.
If you have received this email in error, please notify the sender immediately 
and delete it from your system. The views expressed in this message do not 
necessarily reflect those of Atos Euronext Market Solutions.

Atos Euronext Market Solutions Limited - Registered in England & Wales with 
registration no. 3962327.  Registered office address at 25 Bank Street London 
E14 5NQ United Kingdom. 
Atos Euronext Market Solutions SAS - Registered in France with registration no. 
425 100 294.  Registered office address at 6/8 Boulevard Haussmann 75009 Paris 
France.

L'information contenue dans cet e-mail est confidentielle et uniquement 
destinee a la (aux) personnes a laquelle (auxquelle(s)) elle est adressee. 
Toute copie, publication ou diffusion de cet email est interdite. Si cet e-mail 
vous parvient par erreur, nous vous prions de bien vouloir prevenir 
l'expediteur immediatement et d'effacer le e-mail et annexes jointes de votre 
systeme. Le contenu de ce message electronique ne represente pas necessairement 
la position ou le point de vue d'Atos Euronext Market Solutions.
Atos Euronext Market Solutions Limited Société de droit anglais, enregistrée au 
Royaume Uni sous le numéro 3962327, dont le siège social se situe 25 Bank 
Street E14 5NQ Londres Royaume Uni.

Atos Euronext Market Solutions SAS, société par actions simplifiée, enregistré 
au registre dui commerce et des sociétés sous le numéro 425 100 294 RCS Paris 
et dont le siège social se situe 6/8 Boulevard Haussmann 75009 Paris France.
=========================================

_______________________________________________
Perl-Unix-Users mailing list
Perl-Unix-Users@listserv.ActiveState.com
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs

Reply via email to