Hendricks Paul D A1C 27 IS/INYS wrote:
> Hello beginners!
> 
> Where do I start...  I've already done some reading on this and I've
> played tag the wall with the forehead long enough.

Are you working through a book, like Learning Perl? If not, you should,
because it takes you step by step through these concepts.

> What I'm challenging myself to do is to create:
>    a)  a way to name a hash from user input

No. Don't do that. Instead, think about what a hash is: a set of key/value
pairs, where you can quickly access a value by its key. keys are always
strings, and values are always scalars. The scalar value can be a reference
to some other structure (hash, array, etc.), which is how you can manage
complex data structures.

So, "name a hash from user input" should be reformulated as: "Get a string
(hash key) from user input and store a reference to another hash using that
key".

Here's a complete example:

     #!/usr/bin/perl

    use strict;
    use Data::Dumper;

    my %data;           # here will be the hashes of user data

    print "Enter your first name: ";
    chomp(my $name = <STDIN>);
    print "Enter your age: ";
    chomp(my $age = <STDIN>);
    print "Enter your favorite color: ";
    chomp(my $color = <STDIN>);

    $data{$name} = { age => $age, color => $color };

    print Dumper(\%data);

Sample run:

   Enter your first name: Bob
   Enter your age: 41
   Enter your favorite color: Green
   $VAR1 = {
             'Bob' => {
                        'color' => 'Green',
                        'age' => '41'
                      }
           };

So we've "named" a hash 'Bob'. The hash has two entries (key 'age', value
'41' and key 'color' value 'Green'). We don't have a variable named %Bob in
our program, but you don't want to do that. Not all strings the user might
enter are legal variable names, and some would conflict with the variables
in your program. So we keep these hashes in a special "namespace", the %data
hash.

>    b)  find a way to create a multi-level hash (hash within a hash)

We just did it. %data is a hash whose value is another hash.

The key line in the example above is:

    $data{$name} = { age => $age, color => $color };

You need to understand exactly what that is doing. Do you?

> 
> From what I can derive, you can insert an array as an element of a
> hash, thus making it multi-dimensional.

Technically, an array *reference*

> 
> As a side effort, which is probably easier...
> I'm also asking for an example of multi-dimensional arrays, and the
> object-orientation aspect. I realize the arrays @array1 and @array2
> will actually be slice numbers 0 and 1, but this is for
> understandability I deviate.
> 
> 
> #Create a multi-dimensional array
> 
> my @parent= (@array1,
>                 @array2);
> 
> #Spit out the info in the arrays
> 
> foreach $i ($parent($array1)) {
>   print "$parent($array1[$i]) is part of $parent($array2[$i])\n"; }

Sorry, I can't figure out at all what you're trying to do here. The first
statement does not create a multi-dimensional array.

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to