View Full Version : assign hash value to new hash in Struct
vietboy505
04-07-2006, 07:15 PM
#!/usr/local/bin/perl
struct Cat => {
name => '$',
age => '$',
owner => '$',
number => '$',
location => '%',
loc => '$'
};
sub main {
my %cat=();
$number=1;
$xyz=334;
$cat{$number}=Cat->new();
$cat{$number}->name("Bob");
$cat{$number}->age("2");
$cat{$number}->owner("Paul G.");
$cat{$number}->number('$number');
$cat{$number}->loc($xyz);
##$cat{$number}->{$location{$number}($xyz)};
}
exit &main;
How can I assign hash value to hash value in Struct?
In scalar, I can assign it as $cat{$number}->loc($xyz); .. But what about the array? --> something like this? ##$cat{$number}->{$location{$number}($xyz)};
Any help would be appreciate.. Thank you.
KevinADC
04-07-2006, 08:27 PM
are you using a module for the struct part of the script? I don't think perl supports that without a module like Class::Struct. Besides that I can't answer your question because I don't understand what you are asking.
vietboy505
04-07-2006, 09:03 PM
yes i am using use Class::Struct;
KevinADC
04-07-2006, 09:16 PM
I suggest you read the documentation of the module:
http://search.cpan.org/~nwclark/perl-5.8.8/lib/Class/Struct.pm
maybe it will help.
vietboy505
04-07-2006, 10:04 PM
I did see assigning for hash, but it's just a scalar to hash.
$hash_ref = $obj->h; # reference to whole hash
$hash_element_value = $obj->h('x'); # hash element value
$obj->h('x', 'new value'); # assign to hash element
But I can't find anything relate to:
Maybe like this?
StructureObj{scalar}->(hash['scalar'],scallar}) ?
##$cat{$number}->($location['$number'],$xyz);
EDIT:
I just try to run the code and got "Not a CODE reference"
All I want is store the value $xyz in the %location hash at that $number.
If it's were array, it would be $location['$number']=$xyz;
If I try to use:
$cat{$number}->{$location{$number}}[$xyz];
$cat{$number}->{ $location{$number}{($xyz)} };
print "Info?" . $cat{$number}->{$location{$number}}[$xyz] . "\n";
print $cat{$number}->name . "\n";
print $cat{$number}->loc . "\n";
I will get the output,
Info?
Bob
334
So I think the location never get assign.
KevinADC
04-07-2006, 10:48 PM
well this will not work:
$location['$number']
the single-quotes around $number kill the variable interpolation, try without the quotes:
$location[$number]
all though I am still very unclear as to what you are trying to do.
vietboy505
04-07-2006, 11:30 PM
$cat{$number}->loc($xyz);
This will assign loca to 334.
print $cat{$number}->loc . " $number $xyz \n";
Output: 334 1 334
That work..
It's just like assigning the variable to the structure values.
But I can't do it hash.
I even try:
$cat{$number}->{$location{$number}}=$xyz;
print "Info? " . $cat{$number}->{$location{$number}} . "\n";
Output:
Info?
I am trying to assign $xyz to $location[$number] since location is a hash.
vietboy505
04-13-2006, 06:40 PM
The solution was:
$cat{$number}->{$location[$number]}=$xyz;
Now I was wondering how do I get the keys back of %location?
##Not work because %location is a hash of Struct..
foreach $numberKey (keys(%location)) {
print $numberKey . "\n";
}
Any idea? Thanks.
KevinADC
04-13-2006, 10:24 PM
Sorry, but I really have no experience with Structs and I don't want to read the documentation to try and figure it out.
vietboy505
04-14-2006, 07:57 PM
$cat{$number}->{$location{$number}}=$xyz;
I can get this to assign the value to the hash, it no matter what the keys.. It's always have the same values.
how can I fix that each $number have a unique key inside %location?
vBulletin® v3.8.2, Copyright ©2000-2012, Jelsoft Enterprises Ltd.