Questions tagged [perl-hash]

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

64 questions
27
votes
4 answers

How can I sort a Perl hash on values and order the keys correspondingly (in two arrays maybe)?

In Perl, I want to sort the keys of a hash by value, numerically: { five => 5 ten => 10 one => 1 four => 4 } producing two arrays: (1,4,5,10) and (one, four, five, ten) And then I want to normalize the values array such that the numbers…
Gogi
  • 1,625
  • 4
  • 19
  • 36
6
votes
3 answers

Perl multi-dimensional table with headers

I'm trying to implement a multi-dimensional table with headers. Here's an example for 2D: < dimension1 > /\ 'column0' 'column1' dimension0 'row0' data00 data10 \/ 'row1' data01 …
Giovanni Funchal
  • 8,295
  • 12
  • 54
  • 103
5
votes
3 answers

Perl Delete Base Key through Hash Reference

my %myHash = ( key1 => { test1 => 1, test2 => 2, }, key2 => { test1 => 3, test2 => 4, }, ); my $myRef = $myHash{ "key". ((~~keys %myHash) + 1) } //= { test1 => 5, test2 => 6, }; Humor me…
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
  • 713
  • 5
  • 21
4
votes
2 answers

perl - operations allowed on array while iterating through it

What are the operations allowed on array, while iterating through it? Is it possible to shift/unshift, pop/push, delete elements without confusing the iterator? Is that any different for adding/removing key-value pair from hash? Thank you for your…
mjp
  • 205
  • 2
  • 11
4
votes
3 answers

Perl: Is this a correct way of creating an unique array?

I'm trying to create an unique array regardless of its original order and using no module, this's what I've come up with so far: my @arr = qw(b a a c d g e f); my %hash; @hash{@arr}=(); say keys %hash;
bolbol
  • 315
  • 3
  • 9
4
votes
2 answers

Sort Perl hash from largest to smallest

I am looking at an example found here: http://perlmeme.org/tutorials/sort_function.html And it gives this code to sort a hash based on each key's value: # Using <=> instead of cmp because of the numbers foreach my $fruit (sort {$data{$a} <=>…
searayman
  • 470
  • 2
  • 8
  • 24
3
votes
3 answers

Perl Hash References - Is it possible to put reference to nested hash into 1 variable?

I have a partially nested hash like the following: $href = {one=>1, word_counts=>{"the"=>34, "train"=>4} }; and I would like to get the value of $href->{'word_counts'}{'train'}. Is it possible to put the {'word_counts'}{'train'} into a variable, so…
jzjzjzjz
  • 43
  • 3
3
votes
4 answers

How to turn a perl hash of an html form into a series of scalar variables?

I'm getting input from an html form. There are a bunch of text inputs, thus a bunch of key-value pairs. You see my current method is excruciatingly tedious when one has more than three pairs. I'd like to know, is there a more efficient method of…
djeikyb
  • 4,000
  • 3
  • 32
  • 37
3
votes
2 answers

Perplexed with merging hashes output using perl

Why does the output vary for merged hashes through several times executing this program? use strict; use warnings; my %data1=(a=>'1',b=>'2',c=>'3'); my %data2=(d=>'4',e=>'5',f=>'6'); my %data3=(%data1,%data2); while(my($key,$value)=each %data3) { …
Sana
  • 25
  • 7
2
votes
2 answers

What is a difference while assigning hash in list context?

I have to expressions: %MON = months => 1, end_of_month => 'limit'; # months => undef %MON = ( months => 1, end_of_month => 'limit' ); Why first expression results only one key months with undef value? What is the difference between them?
2
votes
2 answers

Can the Perl hash name (at the time of declaration) be used inside same hash?

I am declaring a hash and at the time of its declaration I am using it inside for one of its element as input to other element. You may grasp easily by following code which is not compiling as Strict pragma is ON: my %cob = ( 'a' => 0, …
2
votes
1 answer

perl list context of hash refs

Why does this work? that is line 2 DB<1> $a = {'a'=>1}; $b = {'a'=>2, 'b'=>0}; DB<2> $c = ($a, $b); DB<3> print $c; HASH(0x8743e68) DB<4> print $c->{a},$c->{b}; 20 I understand if I carefully use %$a and %$b that perl would know what I meant,…
Don
  • 4,245
  • 24
  • 33
2
votes
4 answers

Tie::IxHash ordered associative arrays in Hash of Hashes?

How can I preserve the order in which the hash elements were added FOR THE SECOND VAR ? ( Hash of Hashes ) For example: use Tie::IxHash; my %hash; tie %hash, "Tie::IxHash"; for my $num (0 .. 5){ $hash{"FirstVal$num"}++; } for my $num (0 .. 5){ …
YoDar
  • 377
  • 6
  • 15
2
votes
2 answers

Adding multiple values to key in perl hash

I need to create multi-dimensional hash. for example I have done: $hash{gene} = $mrna; if (exists ($exon)){ $hash{gene}{$mrna} = $exon; } if (exists ($cds)){ $hash{gene}{$mrna} = $cds; } where $gene, $mrna, $exon, $cds are unique ids. But, my…
aki2all
  • 349
  • 1
  • 5
  • 21
1
2 3 4 5