perl ref to hash with refs in it (how to get what's being referenced).


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting perl ref to hash with refs in it (how to get what's being referenced).
# 1  
Old 08-23-2011
perl ref to hash with refs in it (how to get what's being referenced).

I have a reference to a hash that contains some references. I was just wondering if there was a more simplistic Way of dereferencing the contained references without having to assign them to another reference like this:

Code:
my $href = shift; #some hash
my $temp = $href->{element};
print join('',@$temp);


What I want is to avoid copying the reference into $temp and then getting the info it's referencing. I don't think @$href->{element} would work seeing that $href is a reference itself. I'm just all for concise code. If anyone knows of a proper way to do this, I'd appreciate it.

Thanks in advance.
# 2  
Old 08-23-2011
I think you can wrap @ around a statement like @{ ... }
# 3  
Old 08-24-2011
Quote:
Originally Posted by mrwatkin
I have a reference to a hash that contains some references. I was just wondering if there was a more simplistic Way of dereferencing the contained references without having to assign them to another reference like this:

Code:
my $href = shift; #some hash
my $temp = $href->{element};
print join('',@$temp);

What I want is to avoid copying the reference into $temp and then getting the info it's referencing. I don't think @$href->{element} would work seeing that $href is a reference itself....
You can write it like this -

Code:
my $href = shift; #some hash
print join('', @{$href->{element}});

Or like this -

Code:
my $href = shift; #some hash
print join('', @{$$href{element}});

An example follows:

Code:
$
$ perl -le '@a = qw(1 10 100); $x{1} = \@a; $href=\%x; print join(" ", @{$href->{1}})'
1 10 100
$
$
$ perl -le '@a = qw(1 10 100); $x{1} = \@a; $href=\%x; print join(" ", @{$$href{1}})'
1 10 100
$
$

tyler_durden
# 4  
Old 08-24-2011
Quote:
Originally Posted by mrwatkin
I have a reference to a hash that contains some references. I was just wondering if there was a more simplistic Way of dereferencing the contained references without having to assign them to another reference like this:

Code:
my $href = shift; #some hash
my $temp = $href->{element};
print join('',@$temp);


What I want is to avoid copying the reference into $temp and then getting the info it's referencing. I don't think @$href->{element} would work seeing that $href is a reference itself. I'm just all for concise code. If anyone knows of a proper way to do this, I'd appreciate it.

Thanks in advance.
To dereference a hash containing references to to other hashes do this...
Code:
$href->{key1}{key2};

now you wont need $temp
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Programming

Perl: restrict perl from automaticaly creating a hash branches on check

My issue is that the perl script (as I have done it so far) created empty branches when I try to check some branches on existence. I am using multydimentional hashes: found it as the best way for information that I need to handle. Saing multidimentional I means hash of hashes ... So, I have ... (2 Replies)
Discussion started by: alex_5161
2 Replies

2. Shell Programming and Scripting

Compare values of hashes of hash for n number of hash in perl without sorting.

Hi, I have an hashes of hash, where hash is dynamic, it can be n number of hash. i need to compare data_count values of all . my %result ( $abc => { 'data_count' => '10', 'ID' => 'ABC122', } $def => { 'data_count' => '20', 'ID' => 'defASe', ... (1 Reply)
Discussion started by: asak
1 Replies

3. Shell Programming and Scripting

Perl hash help

Hi , i have the below code its working fine when i execute in unix , but its not working in windows could you pls explain me where i am going wrong. This is the program $data = { '1' => 'one' , '2' => 'two' , 3 => 'three' }; print "hello : $data->{'1'}... (2 Replies)
Discussion started by: ragilla
2 Replies

4. Shell Programming and Scripting

Basic Perl - pass by ref

#!/usr/bin/perl sub addToHash{ my(%hash) = @_; $hash{ 'A' } = 'value'; $hash{ 'B' } = 'value'; $hash{ 'C' } = 'value'; print("Hashmap size is " .keys(%hash) . "\n"); } my %myhash = (); addToHash(\%myhash); print("Hashmap size is " .keys(%myhash) . "\n"); I want this... (6 Replies)
Discussion started by: chrisjones
6 Replies

5. Shell Programming and Scripting

perl hash - using a range as a hash key.

Hi, In Perl, is it possible to use a range of numbers with '..' as a key in a hash? Something in like: %hash = ( '768..1536' => '1G', '1537..2560' => '2G' ); That is, the range operation is evaluated, and all members of the range are... (3 Replies)
Discussion started by: dsw
3 Replies

6. Shell Programming and Scripting

Perl Hash:Can not keep hash data in the same order that it was inserted

Can Someone explain me why even using Tie::IxHash I can not get the output data in the same order that it was inserted? See code below. #!/usr/bin/perl use warnings; use Tie::IxHash; use strict; tie (my %programs, "Tie::IxHash"); while (my $line = <DATA>) { chomp $line; my(... (1 Reply)
Discussion started by: jgfcoimbra
1 Replies

7. Shell Programming and Scripting

perl hash

This is my data 1 0 1 0 1 1 1 2 1 6 1 7 Assume that first field is key and 2nd field is value I want to create a hash in perl, on this data. My hash should having uniq key and all values by , separated. 1,0,0,1,2,6,7 1 will be my key and rest of are should be values. (3 Replies)
Discussion started by: pritish.sas
3 Replies

8. Shell Programming and Scripting

perl hash

i have an hash table in which each value is an array. How can i print for each key the array values??? something like this: thanks (2 Replies)
Discussion started by: littleboyblu
2 Replies

9. Shell Programming and Scripting

Perl Hash

HI I have a hash like this $hashname->{$filesystem}->{'fsname'}=$filesystem; How to get the values from this multilevel hash. Thanks in advance... :) (1 Reply)
Discussion started by: Harikrishna
1 Replies

10. Shell Programming and Scripting

Hash in perl

Hi Help me with some good links of Hash with in Hash .(Multidimensional hash).. Regards Harikrishna (1 Reply)
Discussion started by: Harikrishna
1 Replies
Login or Register to Ask a Question