In article <[EMAIL PROTECTED]>, John W. Krahn wrote:
> Sharad Gupta wrote:
>>
>> Hi Friends,
>
> Hello,
Hi,
>> I have a hash whose values can be either array, hash etc.
>>
>> say i have:
>>
>> my $self = {
>> 'hello how' => {
>> 'r u' => 'I am',
>> ....
>> ....
>> },
>> 'fine thank' => ['you'],
>> 'see ya' => 'next time',
>> ....
>> ....
>> };
>>
>> How can i replace the spaces in keys with '_' without copying $self to
>> some other place.?
>
> You can't modify the keys once they are created. You would have to
> create a new key and copy the value to the new key.
(I noticed that, too.) But thanks to your tip I think I've created my first
recursive sub-routine (only tested on this example). If it does what the OP
requested (and y'all don't find too much wrong with it) then I'm a happy
man!
pad_keys($self);
print Dumper(\$self);
sub pad_keys {
my $ref = shift;
if (ref $ref eq "HASH") {
for my $value (%$ref) {
if (ref $value eq "HASH") {
pad_keys($value);
}
}
for (keys %$ref) {
my $old = $_;
tr/ /_/;
$ref->{$_} = $ref->{$old};
delete $ref->{$old};
}
}
}
Dump...
$VAR1 = \{
'see_ya' => 'next time',
'fine_thank' => [
'you'
],
'hello_how' => {
'r_u' => 'I am'
}
};
-K
--
Kevin Pfeiffer
International University Bremen
--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]