Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I access array of hash in perl?

I have a big array of hashes, I want to grab some hash from the array and insert into new array without changing the first array. I am having problem pushing the hash to array, how do I access the ith element which is a hash.

my @myarray;
$my_hash->{firstname} = "firstname";
$my_hash->{lastname} = "lastname";
$my_hash->{age} = "25";
$my_hash->{location} = "WI";
push @myarray,$my_hash;

$my_hash->{firstname} = "Lily";
$my_hash->{lastname} = "Bily";
$my_hash->{age} = "22";
$my_hash->{location} = "CA";
push @myarray,$my_hash;

$my_hash->{firstname} = "something";
$my_hash->{lastname} = "otherthing";
$my_hash->{age} = "22";
$my_hash->{location} = "NY";
push @myarray,$my_hash;

my @modifymyhash;
for (my $i=0;$i<2; $i++)  {
        print "No ".$i."\n";
        push (@modifymyhash, $myarray[$i]);
        print "".$myarray[$i]."\n";  #How do I print first ith element of array which is hash.
 }
like image 208
mysteriousboy Avatar asked Mar 19 '13 20:03

mysteriousboy


People also ask

How do I read a hash array in Perl?

A Perl hash is defined by key-value pairs. Perl stores elements of a hash in such an optimal way that you can look up its values based on keys very fast. Like a scalar or an array variable, a hash variable has its own prefix. A hash variable must begin with a percent sign (%).

How do you access the elements of a hash in Perl?

Perl Hash Accessing To access single element of hash, ($) sign is used before the variable name. And then key element is written inside {} braces.

Can a Perl hash value be an array?

You can create arrays of hashes, hashes of arrays, and any other sort of complicated data structure you can dream up. To learn more about these, look at the Perl documentation.

How do I reference a hash in Perl?

Similar to the array, Perl hash can also be referenced by placing the '\' character in front of the hash. The general form of referencing a hash is shown below. %author = ( 'name' => "Harsha", 'designation' => "Manager" ); $hash_ref = \%author; This can be de-referenced to access the values as shown below.


2 Answers

First you should

use strict;
use warnings;

then define

my $my_hash;

initialize $my_hash before you assign values, because otherwise you overwrite it and all three elements point to the same hash

$my_hash = {};

and finally, to access the hash's members

$myarray[$i]->{firstname}

or to print the whole hash, you can use Data::Dumper for example

print Dumper($myarray[$i])."\n";

or some other method, How can I print the contents of a hash in Perl? or How do I print a hash structure in Perl?

Update to your comment:

You copy the hashes with

push (@modifymyhash, $myarray[$i]);

into the new array, which works perfectly. You can verify with

foreach my $h (@myarray) {
    print Dumper($h), "\n";
}

foreach my $h (@modifymyhash) {
    print Dumper($h), "\n";
}

that both arrays have the same hashes.

If you want to make a deep copy, instead of just the references, you can allocate a new hash and copy the ith element into the copy. Then store the copy in @modifymyhash

my $copy = {};
%{$copy} = %{$myarray[$i]};
push (@modifymyhash, $copy);
like image 65
Olaf Dietsche Avatar answered Oct 11 '22 17:10

Olaf Dietsche


To dereference a hash, use %{ ... }:

print  %{ $myarray[$i] }, "\n";

This probably still does not do what you want. To print a hash nicely, you have to iterate over it, there is no "nice" stringification:

print $_, ':', $myarray[$i]{$_}, "\n" for keys %{ $myarray[$i] };
like image 43
choroba Avatar answered Oct 11 '22 17:10

choroba