Questions tagged [hash-reference]

11 questions
64
votes
4 answers

What's the difference between a hash and hash reference in Perl?

I would like to properly understand hashes in Perl. I've had to use Perl intermittently for quite some time and mostly whenever I need to do it, it's mostly related to text processing. And everytime, I have to deal with hashes, it gets messed up. I…
user855
  • 16,878
  • 34
  • 86
  • 143
4
votes
4 answers

What does `$hash{$key} |= {}` do in Perl?

I was wrestling with some Perl that uses hash references. In the end it turned out that my problem was the line: $myhash{$key} |= {}; That is, "assign $myhash{$key} a reference to an empty hash, unless it already has a value". Dereferencing this…
slim
  • 36,139
  • 10
  • 83
  • 117
3
votes
2 answers

Perl Creating hash reference and looping through one element from each branch at a time

As a beginner I have what I think is a rather complicated problem I am hoping someone could help with. I have the following text file (tab delminated)... FILE1.txt Dog Big Dog Medium Dog Small Rabbit Huge Rabbit …
yonetpkbji
  • 989
  • 2
  • 20
  • 32
1
vote
2 answers

Does returning a ref to a hash declared in subroutine causes memory leak?

Is using my %data; below okay or should I switch to my $data={}; ? #!/usr/bin/perl -wT use strict; use JSON; sub makeJson { my %data; $data{kib} = 1; $data{games} = { 0 => [], 1 => [qw(a b c…
Alexander Farber
  • 18,345
  • 68
  • 208
  • 375
1
vote
3 answers

How to display the special value always at top while sorting?

I used the below code to sort values and display it as a dropdown in a Perl form page And I need to display a certain value always at the top of the sorted list, how to do that? values= [sort {$a<=>$b and $orig->{$a} cmp $orig->{$b}} keys %$orig]…
1
vote
1 answer

Error when accessing array of hashes

I'm trying to generate a list of hashes, interacting with the user using the following script: use strict; use warnings; my $KEY_1 = "one"; my $KEY_2 = "two"; sub generateHash{ my ($value1, $value2) = (@_); $value2 = $value1 + 5.0; …
Hellzzar
  • 165
  • 8
1
vote
2 answers

Accessing args in Perl Hash Ref

$self->doSomething({ record => $record, listing => [ $foo, $bar, $baz ], passedargs => { something => $val, another => $val2 } }); accessing args within a hashref. So if I wanted to access record I would my $record =…
1
vote
3 answers

Perl nested data structures

I'm having trouble wrapping my brain around the data structure that is being returned...What I need to do is inspect the results, and modify a field if it is a HASH. Within 'results' any KEY that is a HASH with a KEY of 'null' and value of '1'…
1
vote
2 answers

How can I update a hash value using a hash reference in Perl?

Is there a way to update a value in a hash using a hash reference that points to the hash value? My hash output looks like this: 'Alternate' => { 'free' => '27.52', 'primary' => 'false', 'used' => '0.01', 'name'…
user2063351
  • 435
  • 2
  • 8
  • 27
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' => { …
-4
votes
1 answer

Passing hash reference to multiple functions in Perl

I have following code in Perl where a hash reference is passed from main function to func1 and then to func2. In func2 hash is updated. I want to access the updated hash in main function. Also there is a while loop in main function and i am…
Sam
  • 233
  • 1
  • 7
  • 22