Weird Hash w/ n-dimensional two-dimensional array !!!!!! 
Author Message
 Weird Hash w/ n-dimensional two-dimensional array !!!!!!

TO ALL:

  First off, let me apologize for this code.  I am a c++ (Win-MFC mainly
(sorry)) - Due to need, I am helping in writing an installer for a large
project for a major company.  I am not sure of "disclosure" so I keep
putting in "xxx" and the like to "hide" names. (>>sorry<<).  I have never
before 2 days ago written in Perl (much less tried to program under UNIX - I
am doing the code in CodeWrite in Win and saving to a Samba drive, I don't
know VI very well at all).

  I am trying to learn as I go - so there are awkward constructs galore! - I
also get an idea and then try too look up in the two O'Reilly books how to
do it (Therefore I THINK very un-Perl-like and try to fit square peg in
round hole at times.)  I am only up to page 70 in the "Learning" book. So
again, I'm sorry.

  The section of code works so far (actually I haven't looked at not
allowing duplicate 1st array vals in the hash, the "name" - but I'm sure I
did a tiny flub, I had it working in a previous incarnation before I ran
into my major prob!!!!!)

The PROB:

  I have a hash of questions to ask the user.  Look at the first lines for
an example of ONE question (for this module there will be 10 or so).  The
two "y" vals are whether the answer should be verified (necessitating a hard
link between question included and the verification code. And whether the
question is asked until the user hits enter on a blank line.

The reading of questions and enforcing of two answers works.

    Finally,  I need to construct an object,  It will contain a Key value
(the ini variable key, so to speak, and the list of answers (in this case a
name and pw).

  So I envisioned:

%somehash = ( theKey => [ [answer1Name, answer1pwd], [answer2Name,
answer2pwd], ... [answerNName, answerNpwd]] );

(if I "manually were to create it...)

AFTER MUCH playing/reading - I got that to be assigned/created properly and
tested thru prints.  BBBBUUUUUTTTTTT , I can't seem to find out some way of
iterating thru this construct (so-called) to "list" all values.  I can do so
only (as you'll see some of the "debug" prints I did) do so if I loop on the
Keys and assum that the last array indices are "0" & "1" (I can "live" with
that assumption) But, I can find no way to detect how many, for a given hash
key, answers there are - Will I have to check until the values are "" (I
don't even know if Perl guarantees that an unassigned array index will be so
(though, now as I say it, I think I read that...)).

  P L E A S E   HELP!  - I am going on vacation Fri, and I need the skeleton
of one of these done, so I can spread out the work and continue (on paper)
while I vacation overseas.

  AS SOON as anyone can clue me in - please do  => Again I am not even
consisten in including scalars in "Value is $something" print statements,
what hope do I have?

Thanks o' Perl-Community-at-large,
Dan Delgado


P.S. => WOW, anyone ?read? this far... (it's late - I got to be in in 4
hours, I ramble...)

THE "CODE":

Quote:
>I include this from  a file:

%XXX_questions =
(
 XXXXX => ["y", "y", "Please type in the names and passwords the XXXXXXX
modules will use to log into the XXXXXX.\n(Format: <XXXXNAME>  <XXXXX
password>)\n"]
);

#!/usr/bin/perl

# REMOVED
#  REMOVED
#
# NOTE: Validation routine requires updating of this file if the
XXX_questions.inc changes.
#
if ($no_questions = $ARGV[0] =~ /noquestions/i)
{  
 require '.XXXX_installrc';

Quote:
}

else
{
 require 'XXXX_questions.inc';
 %answers = ();
Quote:
}

unless ($noquestions)
{
 while (($key, $question_info) = each %XXX_questions)
 {
  $question = $XXX_questions{$key}[2];
        $multiple = $XXX_questions{$key}[1];
  $check    = $XXX_questions{$key}[0];
  # Loop at least once or until a blank line is entered if it is a
  # multiple answer question
  for ($index = 0; ($index == 0) || ($multiple eq "y") ; $index++)
  {   print $question;
   chomp( $input = <STDIN> );

   # Entering an empty line will
 end the loop
   if ($#answer > -1) # if empty
   {
    if ( $check eq "y" )
    {
# ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

     {

     }
# ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    }
    else
    {

    }
   }
   else
   {
    last;
   }
  }
 }

Quote:
}

# ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

foreach $key (keys(%answers))
{


print "Number of items: $hope[0] or" . $hope;
#print "\n" . $answers{$key};

print "\n\n";
print "\nNum: " . $answers{$key};
print "XXX" . $answers{$key}[0][0];
print "XXX" . $answers{$key}[0][1];
print "\nNum: " . $answers{$key};
print "XXX" . $answers{$key}[1][0];
print "XXX" . $answers{$key}[1][1];
print "\nNum: " . $answers{$key};
print "XXX" . $answers{$key}[2][0];
print "XXX" . $answers{$key}[2][1];

Quote:
}
}

# ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

sub check_answer
{

 local ($input);


 if ($key eq "XXXXX")
 {
  # Global %answers
  while ( $#answer != 1) # (2 items)
  {
   print "\nPlease re-enter values (Format: <XXXXXNAME>  <XXXXX
password>)\n";
   chomp ( $input = <STDIN> );

  }
  # Should we allow only a difference in the case or append 'i' to regexp?

  {
   if ( get_yn('Warning Duplicate XXXXX name: Do you wish to replace the old
value? (y/n) ') )
   {
    # Replace XXXX's password value
    return 1;
   }
   else
   {
    # Keep old password value
    return 0;
   }
  }
  return 1;
 }

Quote:
}

#
# Get_yn() returns 1 for a "y" or "yes" (irregardless of case)
#
sub get_yn()
{
 print $_[0];
 chomp( $_ = <STDIN> );
 return ( /y|yes/ );
Quote:
}



Sun, 26 Mar 2000 03:00:00 GMT  
 
 [ 1 post ] 

 Relevant Pages 

1. 2-dimensional array into 1 dimensional array?

2. Need Help with putting Hashes in a two-dimensional array

3. Associative array of a two dimensional array

4. two dimensional hashes

5. Reading into a two-dimensional array from STDIN

6. two-dimensional arrays

7. how to build two dimensional array?

8. how to build two dimensional array?

9. Two dimensional array

10. Different ways of initialising a two dimensional array

11. Dynamic Two-Dimensional Array Creation?

12. two-dimensional arrays, lists, whatever

 

 
Powered by phpBB® Forum Software