Re: How to dereference when hash key is referenece
by davorg (Chancellor) on Apr 26, 2006 at 10:56 UTC
|
The keys of a hash are strings. Any value that you use as the key of a hash is converted to a string. Therefore, if you use a reference as the key to a hash then it loses its "reference-ness" and can no longer be used as a reference.
This is almost certainly a clue that you're trying to do the wrong thing. As GrandFather said, if you step back and explain what you're trying to achieve, we can probably help you achieve it without fighting against Perl.
--
< http://dave.org.uk>
"The first rule of Perl club is you do not talk about
Perl club." -- Chip Salzenberg
| [reply] |
Re: How to dereference when hash key is referenece
by BrowserUk (Patriarch) on Apr 26, 2006 at 11:14 UTC
|
You could use Tie::RefHash which preserves the dereferencability of the hash key references (by storing them in a parallel array if I recall correctly.
Or you could use Devel::Pointer to unsmash the numbers from the keys back into usuable references. This requires that you remember what type of reference that you smashed in teh first place. Use with care :)
Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
Lingua non convalesco, consenesco et abolesco. -- Rule 1 has a caveat! -- Who broke the cabal?
"Science is about questioning the status quo. Questioning authority".
In the absence of evidence, opinion is indistinguishable from prejudice.
| [reply] |
Re: How to dereference when hash key is referenece
by blazar (Canon) on Apr 26, 2006 at 10:58 UTC
|
In my code hash variable key is an array reference
No it isn't! It is a stringified version of the reference. In fact in Perl (5) a hash key can only be a string. In the conversion you loose "some" (read: "quite a lot of"!) information, hence the problem you found. The only workaround is to keep track of it yourself, which possibly amounts to useless duplication, or else completely change the logic of your code, which seems more reasonable.
| [reply] |
Re: How to dereference when hash key is referenece
by GrandFather (Saint) on Apr 26, 2006 at 11:05 UTC
|
#!/usr/local/bin/perl
use strict;
use warnings;
my $array_ref = [1, 2, 5, 7, 4];
my %hash;
$hash{join $;, @$array_ref} = 'ashok';
for my $as (keys %hash){
print join ',', split /$;/, $as;
}
Prints:
1,2,5,7,4
as you wanted. This works by creating a string that contains the array values and uses the string as the hash key. The split pulls the key string back apart for printing.
This is very likely not a good solution to your actual problem however!
DWIM is Perl's answer to Gödel
| [reply] [d/l] [select] |
|
|
If in the x part of his question $array_ref is ment to store only integers then this is ok.
You have a problem, when an array element contains the $; char and you convert string -> array(ref).
| [reply] |
|
|
I doubt very much that the question asked was the real question. To that extent any answer is bogus and refinements, while usefull for teaching in a broader sense, are not actually addressing OP's real problem.
I consider that the code I posted as an answer to the question asked to be sub-optimum in many ways, but unless we can figure out what OP is trying to do, we probably can't do much better.
DWIM is Perl's answer to Gödel
| [reply] |
Re: How to dereference when hash key is referenece
by punkish (Priest) on Apr 26, 2006 at 17:45 UTC
|
Ashok,
Here is another explanation, or rather, a visual of what is going wrong with your code above --
#!perl -w
use strict;
use warnings;
use Data::Dumper;
my $array_ref = [1, 2, 5, 7, 4];
my %hash;
$hash{$array_ref} = 'ashok';
print Dumper($array_ref);
print Dumper(\%hash);
------ output --------
$VAR1 = [
1,
2,
5,
7,
4
];
$VAR1 = {
'ARRAY(0x2253b8)' => 'ashok'
};
As you can see, in your hash, you have unintentionally tried to create a key using an array... as others have said, a key can only be a scalar. Whenever in doubt, try to use Data::Dumper to dump out what Perl is encountering. It is like debugging, without the debugger (wishes debugger were easier).
Good luck.
--
when small people start casting long shadows, it is time to go to bed
| [reply] [d/l] |
Re: How to dereference when hash key is referenece
by njcodewarrior (Pilgrim) on Apr 26, 2006 at 11:07 UTC
|
Ashok,
There are a couple of errors here:
1. You're using the array reference ( and thus, the entire list) as a key to your hash with a value of 'ashok'. This is backwards. I think (correct me if I misunderstood) you want to use 'ashok' as the key to the hash whose value is the array ref.
2. Change the inner for loop ( I'd use foreach instead, but that's a matter of personal preference) to refelect the changes suggested in (1).
Try this:
#! /usr/bin/local/perl
use strict;
use warnings;
my $array_ref = [1, 2, 5, 7, 4];
my %hash;
$hash{'ashok'} = $array_ref;
foreach my $as ( keys %hash ) {
foreach ( @{ $hash{$as} } ) {
print "$_, ";
}
}
Hope this helps.
| [reply] [d/l] |
Re: How to dereference when hash key is referenece
by billh (Pilgrim) on Apr 26, 2006 at 11:06 UTC
|
hash keys are strings.
anything you use as a hash key will have its "string value" used instead.
the "string value" of your $array_ref is "ARRAY(0x8831180)" | [reply] |
Re: How to dereference when hash key is referenece
by GrandFather (Saint) on Apr 26, 2006 at 10:46 UTC
|
What are you trying to achieve? Ask the x question, not the y question!
DWIM is Perl's answer to Gödel
| [reply] |
|
|
Ask the x question, not the y question!
Anyone who has bothered to memorize the meaning of that idiomatic, crypic phrase won't be anyone who needs to be told what it implies in English. Anyone who hasn't, will.
Let's try to drop the elitist jargon, shall we? It serves no purpose in this case, and little purpose in any other case.
| [reply] |
|
|
You might have noticed the first part of GrandFather's reply was an easily understandable, plain English question. Hopefully we can at least give the poor, naive question asker the benefit of the doubt in his ability to read.
As for serving no purpose, I guess that depends on who the purpose is intended to serve. It may not help the original poster much, but it should help other potential responders. It's like raising a red flag for other qualified help representatives, saying, "we have seen this type of thing before, please look to your previous experience to guide you in helping this individual." Was that plain English enough for you?
| [reply] |
|
|
| [reply] |