Home > database >  How to iterate through an array of hashes inside an object with perl
How to iterate through an array of hashes inside an object with perl

Time:03-10

I am trying to iterate through an array of hashes stored as the '_skills' attribute of a 'Person' object, and having trouble.

test1.pl

my $person = new Person( "Hubert", "Blu",
    [
       {"name" => "Python", "Weight" => 80, "group","IT SKILLS", "id"=>0002},
       {"name" => "Magic", "Weight" => 100, "group", "MISC SKILLS", "id"=>0001},
       {"name" => "JavaScript","Weight" => 70, "group"=> "IT SKILLS", "id"=>0004},
       {"name" => "Mage","Weight" => 100, "group"=>"JOB TITLE", "id" =>0003},
       {"name" => "Assassin", "Weight" => 85, "group"=>"JOB TITLE"}
    ]
);


and Person.pl

sub new {
    my ($class,$first_name,$last_name,@skills) = @_ ;
    my $self = {};
    $self->{"_first_name"} = $first_name;
    $self->{"_last_name"} = $last_name;
    @{$self->{"_skills"}} = @skills;
    # Print all the values just for clarification.
    print "First Name is $self->{_first_name}\n";
    print "Last Name is $self->{_last_name}\n";
    print "Skills : " . Data::Dumper::Dumper($self->{_skills}) . "\n";


    print"---SKILLS---\n";
    my $i = 1;
    foreach my $skill_set (@{$self->{_skills}}) {
        print "skill ".$i .": \n";
        $i  ;
        #print Data::Dumper::Dumper($skill_set) . "\n";
        print "$skill_set->{'_first_name'}";
    };
    bless $self, $class;
    return $self;
}

the first dump shows the array and it looks fine. the second dump inside the loop displays the entire array again, and it only loops through once before finishing with the following error

Not a HASH reference at /home/izoom/PerlTrainingGround/LeeR/lib/Person/Person.pm line 26.

Been stuck for hours any help appreciated as always!

CodePudding user response:

You are passing three arguments to new plus the invocant. The last is a reference to an array. That means that @skills ends up with a single element whose value is that reference.

my $person = new Person( "Hubert", "Blu",
    [
       {"name" => "Python", "Weight" => 80, "group","IT SKILLS", "id"=>0002},
       {"name" => "Magic", "Weight" => 100, "group", "MISC SKILLS", "id"=>0001},
       {"name" => "JavaScript","Weight" => 70, "group"=> "IT SKILLS", "id"=>0004},
       {"name" => "Mage","Weight" => 100, "group"=>"JOB TITLE", "id" =>0003},
       {"name" => "Assassin", "Weight" => 85, "group"=>"JOB TITLE"}
    ]
);

should be

my $person = new Person( "Hubert", "Blu",
   {"name" => "Python", "Weight" => 80, "group","IT SKILLS", "id"=>0002},
   {"name" => "Magic", "Weight" => 100, "group", "MISC SKILLS", "id"=>0001},
   {"name" => "JavaScript","Weight" => 70, "group"=> "IT SKILLS", "id"=>0004},
   {"name" => "Mage","Weight" => 100, "group"=>"JOB TITLE", "id" =>0003},
   {"name" => "Assassin", "Weight" => 85, "group"=>"JOB TITLE"}
);

or

    my ($class,$first_name,$last_name,@skills) = @_ ;

    @{$self->{"_skills"}} = @skills;     # Useless shallow array copy
       # -or-
    $self->{"_skills"} = [ @skills ];    # Same[1] but clearer
       # -or-
    $self->{"_skills"} = \@skills;       # No copy of elements

should be

    my ($class,$first_name,$last_name,$skills) = @_ ;

    @{$self->{"_skills"}} = @$skills;    # Shallow array copy
       # -or-
    $self->{"_skills"} = [ @$skills ];   # Same[1] but clearer
       # -or-
    $self->{"_skills"} = $skills;        # No copy of elements

  1. Since $self->{"_skills"} is undef at this point.
  •  Tags:  
  • perl
  • Related