Questions tagged [hashref]

Use this tag for questions related to a hashref, an abbreviation to a reference to a hash.

Quoting this answer:

A hash is a basic data type in Perl. It uses keys to access its contents. A is an abbreviation to a reference to a hash. References are scalars, that is simple values. It is a scalar value that contains essentially, a pointer to the actual hash itself.

Make sure to use as well in your question.

82 questions
10
votes
4 answers

Reference found where even-sized list expected in Perl - Possible pass-by-reference error?

I have a Perl class/module that I created to display Bible verses. In it there is a hash that stores several verses, with the key being the book/chapter/verse and the value being the text. This hash is returned from the module. I'm including the…
6
votes
2 answers

Type of argument to keys on reference must be unblessed hashref or arrayref

if((scalar keys ($this->{'libraries'}->{$y}->{'cellHash'})) == 0){ This is the line where I am getting the "Type of argument to keys on reference must be unblessed hashref or arrayref" error. Can you help me fix this? I am not posting the code…
Rahul Reddy
  • 12,613
  • 10
  • 22
  • 21
6
votes
2 answers

Perl: How to push key/value pair onto hashref and still retain reference

$a = {b=>{c=>1}}; # set up ref $b = $a->{b}; # ref the ref $b .= (d=>1,e=>1); # where we want to assign multiple key/val at once At the end of it $a should look like: { 'b' => { 'c' => 1, 'd' => 1, 'e'…
vol7ron
  • 40,809
  • 21
  • 119
  • 172
5
votes
5 answers

Is there a Hash::Util alternative for compound hashes?

I have a compound hashref as follows my $ch = { k1 => [ { k=>1 }, { m=>2 } ], k2 => [ { l=>90}, ... ], }; Hash::Util::lock_hashref_recurse($ch) does not effectively lock these values.. @{$ch->{k1}}[0]->{k} = 'New value'; is allowed ! How do…
trinity
  • 10,394
  • 15
  • 49
  • 67
5
votes
3 answers

Declare and populate a hash table in one step in Perl

Currently when I want to build a look-up table I use: my $has_field = {}; map { $has_field->{$_} = 1 } @fields; Is there a way I can do inline initialization in a single step? (i.e. populate it at the same time I'm declaring it?)
Brad Mace
  • 27,194
  • 17
  • 102
  • 148
4
votes
2 answers

Perl access value from Hash Reference

In my Perl code, I ended up having a hash reference like below. I would like to access an individual element from it. I tried multiple ways, but I was not able to fetch it. #!/usr/bin/perl #use strict; use Data::Dumper; my…
Vinoth Karthick
  • 905
  • 9
  • 27
4
votes
4 answers

How to get key from the reference of a hash element

suppose $my_ref = \$hash{'mary'}; #my_ref is a reference point to a hash element. .... later on, how can I use $my_ref to retrieve the key of the hash element it point to? i.e how to get string 'mary' from $my_ref? I ask this question because I have…
user399517
  • 3,543
  • 2
  • 19
  • 15
4
votes
2 answers

How to set a list of scalars from a perl hash ref?

How do I set a list of scalars from a perl hash? use strict; my $my_hash = { field1=>'val1', field2=>'val2', field3=>'val3', }; my ($field1,$field2,$field3) = %{$my_hash}{qw(field1 field2 field3)}; print…
null
  • 889
  • 1
  • 13
  • 24
4
votes
1 answer

XML::Dumper using wrong hash reference in output

I'm trying to serialize following data structure using XML::Dumper 'options_settings' => { 'telnet.distinct.enable' => { 'text' => 'Option telnet.distinct.enable needs to be set to \'on\' as of workaround for Bug 476803', 'severity' =>…
3
votes
4 answers

Perl: Create hash of hashes, last key as a reference to an array

http://codepad.org/8fJG5XaB Need a little help creating hashrefs of hashrefs, with the last key as a reference to an array. use Data::Dumper; my $foo = "a:b:c:d:a"; my $bar = "a:b:c:d:z"; my $hoh = {}; sub createHash { my…
vol7ron
  • 40,809
  • 21
  • 119
  • 172
3
votes
5 answers

How do I convert an array or a list into a hashref?

I have a list like this: my $myV3VersionOfData = ["ZG","ZB","CXLDN",...]; and I want to convert it into a dictionary like this: my $entries = { 'ZG' => { 'value' => 'ZG' }, 'ZB' => { 'value' => 'ZB' }, 'CXLDN' => { 'value' => 'CXLDN' }, ... }; I…
raxchi
  • 43
  • 8
3
votes
1 answer

Syntax error in hashref lookup, can not see why

perl -E 'say for map s/(æ|ø|å)/ {qw(æ ae ø oe å aa)}->{$1}/ger, qw(rød gul blå)' perl -E 'say for map s/(æ|ø|å)/"".{qw(æ ae ø oe å aa)}->{$1}/ger, qw(rød gul blå)' The first line above gives me syntax error at -e line 1, near "}->" but the second…
Kjetil S.
  • 3,468
  • 20
  • 22
3
votes
2 answers

How can i solve Uncaught TypeError: Cannot read property 'top' of undefined

i have one page website. i added smoothscroll with js. $(document).ready(function(){ $("li > a").on('click', function(e) { if (this.hash !== "") { e.preventDefault(); var hash = this.hash; $('html, body').animate({ …
Reza
  • 119
  • 2
  • 9
3
votes
3 answers

perl group results in hash using fetchrow_hashref

I want to group my results by countryid with the data shown below. my @test = (); my $st = qq[ SELECT id,countryID,abbrev FROM myTable ]; my $q = $data->prepare($st); $q->execute() or query_error($st); while ( my $result =…
ak85
  • 4,154
  • 18
  • 68
  • 113
3
votes
2 answers

How can I get rid of the "Can't locate object method "warn" via package "sssself" error in IE::Mechanize?

I'm playing around with the Win32::IE::Mechanize. I'm trying a script to automatically access six of my web-based email accounts. The script basically works but perl throws a kind of cryptic "Can't locate object method "warn" via package "sssself"…
Mike
  • 1,841
  • 5
  • 24
  • 34
1
2 3 4 5 6