Questions tagged [perl-hash]

refers to the hash variable type in Perl. Other languages call this a dictionary or associate away.

64 questions
1
vote
1 answer

Cannot fire subroutine inside a hash of subroutines in perl

Hi I'm quite new to perl. I have a perl hash containing subroutines. I've tried to run it in various ways that I found online. But nothing seems to work. My code : %hashfun = ( start=>sub { print 'hello' }, end=>sub { print 'bye' } ); And I've…
Ihsan Izwer
  • 123
  • 1
  • 10
1
vote
1 answer

dereferencing nested hash perl

If I declare a hash like this: my %n = (a => {1 => "1a",2 => "2a"},"b" => {1 => "1b",2 => "2b"}); How do I access the data in: n -> a -> 1? I tried print "$n{a{1}}"; print "$n{a ->{ 1}}"; These two don't work
Tapajit Dey
  • 1,048
  • 2
  • 10
  • 20
1
vote
1 answer

How to copy a nested hash

How to copy a multi level nested hash(say, %A) to another hash(say, %B)? I want to make sure that the new hash does not contain same references(pointers) as the original hash(%A). If I change anything in the original hash (%A), it should not…
gsinha
  • 1,073
  • 2
  • 16
  • 36
1
vote
1 answer

Perl hash giving undef values

If was trying to write a piece of code using perl hash. There are no undefined values or new lines in __DATA__(tried giving same input from file also). But while printing using data dumper or the traditional way I am getting a '' as key and undef…
aravind ramesh
  • 307
  • 2
  • 11
1
vote
1 answer

Accessing elements in hash of hash of arrays in perl

my %PlannedPerWeek = ( September => { Week1 => [80, 23, 199, 45, 19, 36], Week2 => [78, 21, 195, 43, 18, 36], Week3 => [76, 19, 191, 41, 17,…
Ram
  • 1,043
  • 3
  • 15
  • 30
1
vote
3 answers

How do I assemble a hash of hashes piece-wise, not up-front?

I want to create a structure of hashes within hashes in Perl, but all tutorials (like chapter 9.4. Hashes of Hashes in Programming Perl) add them all upfront. I want to initially create the structure and then fill in the empty hashes using…
0
votes
3 answers

Shorten code: merge arrays from hashes

I have a list of hashes and some of the hashes contain a key which provides an array itself. my @cars = ( { # empty car name => "BMW", }, { # car with passengers name => "Mercedes", passengers => [qw(Paul Willy)], }, …
Daniel Böhmer
  • 12,527
  • 5
  • 31
  • 45
0
votes
2 answers

Initializing hash of arrays

I wanted to have a hash of arrays of counters, with initial values of 0. Here is my first attempt at the code: my @names = ("", a, b, c); my %hsh = (); for $i (1..3) { # I expected this line to give me a fresh new array each time - it did not. …
T G
  • 376
  • 2
  • 7
0
votes
1 answer

Perl: How to make hash of hash where keys come from an array

I want to make a hash of hash using the structure of an array. Each array element should be a subkey of the preceding array element. For example using the following arrays: @array1 = ("animal","dog","sparky"); @array2 =…
Westrock
  • 171
  • 4
  • 13
0
votes
2 answers

How to parse multiple csv files with Perl and print only the unique results

I have a bunch of csv file in a simple format say 'Name,Country,Currency'. I need to read all of them and print only the unique union. If they show up in few files they are identical. Tried to use Hash::Merge but seems to be working only for two. I…
rolleikid
  • 13
  • 1
0
votes
2 answers

How can I store one element of a two dimensional in another hash?

I have a two dimensional hash. myHash = ( 'a' => { 'a1' => v1 'a2' => v2 } 'b' => { 'b1' => v3 'b2' => v4 } ); I want to retrieve a hash like this: %hashSlice = %Hash{'a'}; Where hashSlice should…
0
votes
2 answers

Find average of hash values Perl

I am looking to return an average of the hash values (per key) below (grades.txt) Melotti, Suzanne: 100 100 95 95 92 87 Wayne, Bruce: 85 85 85 75 75 75 Stark, Tony: 92 92 75 79 91 87 Odinson, Thor: 23 12 10 42 50 64 Kane, Kathy: 100 100 100 100 95…
0
votes
2 answers

How do I merge two hashes that start with different keys in Perl?

I have to hashes as follows: my %hash1 = ( 'modules' => { 'top0' => { 'instances' => { 'sub_top' => { 'instances' => { 'inst2' => 2, 'inst0'…
0
votes
3 answers

How can I iterate through nested hash of hash without knowing its key?

I have a hash of hashes, that is, hash and hash references in my data structure. Can I iterate through the deepest hash when I only have the main hash name and no key of the deepest hash? my %gates_info=( 'order' => { 'nand' => { …
0
votes
1 answer

Accessing and printing Hash of array

I have a question of how to print a hash: @language(1,2,3); for my $i (0 .. $#language) { $statement = $db->selectall_arrayref( "select word from words left outer join language on words.languageId = language.languageId where…
JoT
  • 303
  • 1
  • 3
  • 10