Title: foreach and an array of hashes
    learn to use Data::Dumper to see what your data looks like.
@emp looks like:
$VAR1 = [
          {
            'position' => 'Cook',
            'age' => 50,
            'name' => 'Bob'
          },
          {
            'position' => 'Receptionist',
            'age' => 20,
            'name' => 'Jane'
          },
          {
            'position' => 'Cashier',
            'age' => 25,
            'name' => 'Joe'
          }
        ];
 
Your print    print $curemp{name}; should look like    print $curemp->{name};
Also might want a linefeed otherwise they all come together.
 
Wags ;)

-----Original Message-----
From: Markham, Richard [mailto:[EMAIL PROTECTED]
Sent: Thursday, March 13, 2003 13:13
To: '[EMAIL PROTECTED]'
Subject: [Perl-unix-users] foreach and an array of hashes

is it possible to foreach through and array of hashes
and reference the objects attributes?
In testing I am just trying to print each employee name.

my @emp;

$emp[0]=new_employee("Bob",50,"Cook");
$emp[1]=new_employee("Jane",20,"Receptionist");
$emp[2]=new_employee("Joe",25,"Cashier");

foreach my $curemp (@emp) {
   print $curemp{name};
}

sub new_employee {
    my ($name, $age, $starting_position) = @_;
    my $r_employee = {                  # Create a unique object
        "name"     => $name,            # using an anonymous hash
        "age"      => $age,
        "position" => $starting_position
    };
    return $r_employee;                 # Return "object"
}

TIA



**********************************************************
This message contains information that is confidential
and proprietary to FedEx Freight or its affiliates.
It is intended only for the recipient named and for
the express purpose(s) described therein.
Any other use is prohibited.
****************************************************************

Reply via email to