Home > Archive > PERL Beginners > January 2008 > Adding a hash to an existing hash?
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
| Author |
Adding a hash to an existing hash?
|
|
| philzenbowl@gmail.com 2008-01-14, 7:02 pm |
| This is the bit of code I'm attempting to use,
"$workPackages{ $tempHash{ WORK_PACKAGE } }
{ $tempHash{ USE_ON_CODE } } = $tempHash{ TECHNICAL_DIRECTIVE };" I
get the following error, "Can't use string ("1") as a HASH ref while
"strict refs" "
Is this because the value stored needs to be the reference to the new
hash? If so, how do I do this? Do I need to reset the original hash
value from 1 to null?
Thanks for any help.
| |
| Gunnar Hjalmarsson 2008-01-14, 7:02 pm |
| philzenbowl@gmail.com wrote:
> This is the bit of code I'm attempting to use,
> "$workPackages{ $tempHash{ WORK_PACKAGE } }
> { $tempHash{ USE_ON_CODE } } = $tempHash{ TECHNICAL_DIRECTIVE };" I
> get the following error, "Can't use string ("1") as a HASH ref while
> "strict refs" "
For me, that piece of code results in a syntax error.
It's also unclear to me what it is you are trying to achieve.
> Is this because the value stored needs to be the reference to the new
> hash?
Well, maybe, a hash value cannot be a hash, but it can be a hash reference.
> If so, how do I do this?
Again, what's "this"? I don't see from the code you posted any attempt
to assign a hash to another hash.
--
Gunnar Hjalmarsson
Email: http://www.gunnar.cc/cgi-bin/contact.pl
| |
| Rob Dixon 2008-01-14, 10:02 pm |
| philzenbowl@gmail.com wrote:
>
> This is the bit of code I'm attempting to use,
> "$workPackages{ $tempHash{ WORK_PACKAGE } }
> { $tempHash{ USE_ON_CODE } } = $tempHash{ TECHNICAL_DIRECTIVE };" I
> get the following error, "Can't use string ("1") as a HASH ref while
> "strict refs" "
>
> Is this because the value stored needs to be the reference to the new
> hash? If so, how do I do this? Do I need to reset the original hash
> value from 1 to null?
You're assigning the value of
$tempHash{TECHNICAL_DIRECTIVE}
to
$workPackages{$tempHash{WORK_PACKAGE}}->{$tempHash{USE_ON_CODE}}
and the error is because
$workPackages{$tempHash{WORK_PACKAGE}}
has the value 1 instead of being a hash reference.
There's little here to help me guess what has gone wrong. Can you tell
us more about your data structures and how they have been built please?
Rob
| |
| philzenbowl@gmail.com 2008-01-15, 7:02 pm |
| On Jan 14, 6:18 pm, rob.di...@350.com (Rob Dixon) wrote:
> philzenb...@gmail.com wrote:
>
>
>
>
> You're assigning the value of
>
> $tempHash{TECHNICAL_DIRECTIVE}
>
> to
>
> $workPackages{$tempHash{WORK_PACKAGE}}->{$tempHash{USE_ON_CODE}}
>
> and the error is because
>
> $workPackages{$tempHash{WORK_PACKAGE}}
>
> has the value 1 instead of being a hash reference.
>
> There's little here to help me guess what has gone wrong. Can you tell
> us more about your data structures and how they have been built please?
>
> Rob
How do I change the value stored to a hash reference? That's really
what I would like to do. Thanks.
| |
| Gunnar Hjalmarsson 2008-01-15, 7:02 pm |
| philzenbowl@gmail.com wrote:
> How do I change the value stored to a hash reference?
C:\home>type test.pl
my %hash = (
key => {
refkey => 'oldvalue',
},
);
print "$hash{key}->{refkey}\n";
$hash{key}->{refkey} = 'newvalue';
print "$hash{key}->{refkey}\n";
C:\home>test.pl
oldvalue
newvalue
C:\home>
--
Gunnar Hjalmarsson
Email: http://www.gunnar.cc/cgi-bin/contact.pl
| |
| Rob Dixon 2008-01-15, 10:02 pm |
| philzenbowl@gmail.com wrote:
> On Jan 14, 6:18 pm, rob.di...@350.com (Rob Dixon) wrote:
>
> How do I change the value stored to a hash reference? That's really
> what I would like to do. Thanks.
Well that's still not very explicit, but lets have a go. Suppose you
have
my $hashref = {
aa => 1,
bb => 2,
cc => 3,
};
and you want to change the value of the element with key 'bb' then you
can write
$hashref->{bb} = 3;
Is this getting close?
Rob
| |
| pzimmermann@gmail.com 2008-01-16, 4:02 am |
| On Jan 15, 10:57 am, nore...@gunnar.cc (Gunnar Hjalmarsson) wrote:
> philzenb...@gmail.com wrote:
>
> C:\home>type test.pl
> my %hash = (
> key => {
> refkey => 'oldvalue',
> },
> );
> print "$hash{key}->{refkey}\n";
> $hash{key}->{refkey} = 'newvalue';
> print "$hash{key}->{refkey}\n";
>
> C:\home>test.pl
> oldvalue
> newvalue
>
> C:\home>
>
> --
> Gunnar Hjalmarsson
> Email:http://www.gunnar.cc/cgi-bin/contact.pl
I know how to change the value stored in hash, that isn't the
problem. Here's a short example of the problem which fails...
use strict;
my %hash;
$hash{ 'key' } = 1;
$hash{ key }{ 'hoh' } = 'test';
| |
| Gunnar Hjalmarsson 2008-01-16, 8:01 am |
| pzimmermann@gmail.com wrote:
> On Jan 15, 10:57 am, nore...@gunnar.cc (Gunnar Hjalmarsson) wrote:
>
> I know how to change the value stored in hash, that isn't the
> problem. Here's a short example of the problem which fails...
>
> use strict;
>
> my %hash;
> $hash{ 'key' } = 1;
>
> $hash{ key }{ 'hoh' } = 'test';
It fails because the value of $hash{key} is not a hash reference. You
still haven't properly explained what it is you want to accomplish... If
you actually want to *replace* the value of $hash{key} with a hash
reference, you can do:
$hash{key} = { refkey => 'test' };
To study the resulting data structure, do:
use Data::Dumper;
print Dumper \%hash;
And to just print the value of the anonymous hash, do:
print "$hash{key}->{refkey}\n";
HTH
--
Gunnar Hjalmarsson
Email: http://www.gunnar.cc/cgi-bin/contact.pl
| |
| Rob Dixon 2008-01-16, 7:02 pm |
| pzimmermann@gmail.com wrote:
> On Jan 15, 10:57 am, nore...@gunnar.cc (Gunnar Hjalmarsson) wrote:
>
> I know how to change the value stored in hash, that isn't the
> problem. Here's a short example of the problem which fails...
>
> use strict;
>
> my %hash;
> $hash{ 'key' } = 1;
>
> $hash{ key }{ 'hoh' } = 'test';
To be able to simplify a question adequately you need to have some idea
of what the answer is going to be. Clearly your expectation is
erroneous. Describe in English what data you want to store without
reference to Perl and we will help.
The value relating to a particular key stored in a hash is a scalar
value. That value can be a simple integer or string, or a reference to a
subsidiary hash or array.
If the value 1 in your code above is meaningless and is simply there so
that you can create the hash element then you can fix things by saying
$hash{'key'} = undef;
which will enable Perl to autovivify the subsidiary hash when you make
the second assignment.
HTH,
Rob
| |
| pzimmermann@gmail.com 2008-01-16, 7:02 pm |
| On Jan 16, 6:21 am, rob.di...@350.com (Rob Dixon) wrote:
> pzimmerm...@gmail.com wrote:
>
>
>
>
>
>
>
> To be able to simplify a question adequately you need to have some idea
> of what the answer is going to be. Clearly your expectation is
> erroneous. Describe in English what data you want to store without
> reference to Perl and we will help.
>
> The value relating to a particular key stored in a hash is a scalar
> value. That value can be a simple integer or string, or a reference to a
> subsidiary hash or array.
>
> If the value 1 in your code above is meaningless and is simply there so
> that you can create the hash element then you can fix things by saying
>
> $hash{'key'} = undef;
>
> which will enable Perl to autovivify the subsidiary hash when you make
> the second assignment.
>
> HTH,
>
> Rob
Thanks Rob, the undef is exactly what I needed! I was originally
creating the hash with a value of "1" which told me that "work
package" would be used. The work package could later not be used at
which point I would change the value to "0". Later I altered my
script to just delete the work package the first instance is failed to
meet certain criteria. As it passed through new filters in the script
(collecting data from an external source), I needed to add additional
information. To get around the this problem I just created a new hash
to store everything. So this now works...
"$workPackages{ $tempHash{ WORK_PACKAGE } }
{ $tempHash{ USE_ON_CODE } . '~~' . $useOnCodeCount } = \%tempHash;"
Thanks again Rob and everyone for the help.
-Philip
|
|
|
|
|