in reply to Scalars as hash keys?

I can answer lines 8 and possibly 26.

In line 8 (the hash declaration, which continues through line 15), you have the curious construct Fastolfe mentioned in passing. The scalar variables in double quotes will be interpolated -- unfortunately, as the variables have never been defined, they throw the undefined value warning on line 8.

I think this may be more to your liking:

my %prompts = ( 'Enter new password' => 'newpass', 'Enter old password' => 'oldpass' ); my %tied_hash; # tie here for my $prompt (keys %prompts) { print $prompt; chomp(my $input = <STDIN>); $tied_hash{$prompts{$prompt}} = $input; }
Does that make more sense? The key in the %prompts hash is the prompt. The value is the slot in the tied hash to which the data entered at that prompt corresponds.

Replies are listed 'Best First'.
RE: Re: Scalars as hash keys?
by Fastolfe (Vicar) on Oct 27, 2000 at 00:26 UTC
    I think he wanted to tie %prompts so that his loop would ask the prompts in the order he defined them above. A normal hash would have asked them all out-of-order...
      Now it's Fastolfe who's reading my mind {g}.   Just encountered "asked them all out-of-order".

      So... 2 questions:
      A - how do I tie my %prompts = (blah blah); ?
      II - does $tied_hash{$prompts{$prompt}} = $input still need to be tied?

      Here's what I've got so far:

      1 #!/usr/bin/perl -w 2 # getpass.pl 3 4 use strict; 5 use Term::ReadKey; 6 use Tie::IxHash; 7 8 # Hash of on-screen prompts for old/new passwords 9 my %prompts = ( 10 'Enter old password:' => 'oldpass', 11 'Enter old enable password:' => 'oldenable', 12 'Enter new password:' => 'newpass', 13 'Confirm new password:' => 'newpassconf', 14 'Enter new enable password:' => 'newenable', 15 'Confirm new enable password:' => 'newenableconf', 16 ); 17 18 # Walk through each on-screen prompt to populate new Hash of passwo +rds 19 print "Prompting for passwords (*won't* appear on-screen)\n\n" 20 my %passwds; 21 for my $prompt (keys %prompts) { 22 print " $prompt " 23 ReadMode('noecho'); 24 chomp(my $input = <STDIN>); 25 $passwds{$prompts{$prompt}} = $input; 26 ReadMode(0); 27 print "\n" 28 }

      In case I haven't already mentioned it, y'all's help is truly appreciated.   Double, no, make that triple++.
          cheers,
          Don
          striving for Perl Adept

RE: (2) Scalars as hash keys? (getting closer 8^)
by ybiC (Prior) on Oct 27, 2000 at 00:30 UTC
    Hmmmm....   I think this resolves my remaining confusion.   Chromatic, you were reading my mind.
    Hope to post working snippet soon.   8^)
        cheers,
        Don
        striving for Perl Adept

    Update: As shown above, I'm still confused.
    Nonetheless, with more research into Tie::IxHash I still hope to post a working snippet soon.