Sun751 has asked for the wisdom of the Perl Monks concerning the following question:

In below code I am iterating my hash and checking if value of res.ldlibrarypath(key) equals to content of $target, Actually I want to substitute some of the content of the value so I am doing substitution, But can any one tell me how can I update my hash after that substitution?
for my $hashref($HR_wnt,$HR_unx,$HR_component) { for my $id (keys %$hashref) { if ($id eq 'res.ldlibrarypath') { if ($$hashref{$id} =~ /=\//) { $$hashref{$id}=~ s/\/opt\/app\/$target.*?://; } } } }
Any one have any suggestion, please Cheers

Replies are listed 'Best First'.
Re: Update hash value
by GrandFather (Saint) on Jun 25, 2009 at 08:43 UTC

    Your two if statements are incompatible - they can never both be true, so no substitution can happen. Aside from that the code works. Consider:

    use strict; use warnings; my $HR_wnt = {'res.ldlibrarypath' => 'Flobble=/opt/app/Flobble:'}; my $HR_unx; my $HR_component; my $target = 'Flobble'; for my $hashref ($HR_wnt, $HR_unx, $HR_component) { for my $id (keys %$hashref) { if ($id eq 'res.ldlibrarypath') { if ($$hashref{$id} =~ /=\//) { $$hashref{$id} =~ s/\/opt\/app\/$target.*?://; } } } } print $HR_wnt->{'res.ldlibrarypath'};

    Prints:

    Flobble=

    Update actually there is nothing wrong with the two if statements. Bogus comment struck, working sample data supplied, commented code reinstated and sample output corrected.


    True laziness is hard work
Re: Update hash value
by moritz (Cardinal) on Jun 25, 2009 at 11:48 UTC
    What you're doing is called "clubbing somebody to death with a loaded Uzi": instead of if iterating over all hash keys and looking op if one is equal to another key, you can simply look up the second key:
    my $key = 'res.ldlibrarypath'; for my $hashref($HR_wnt,$HR_unx,$HR_component) { if (exists $hashref->{$key} && $hashref->{$key} =~ m{/}) { $hashref->{$key} =~ s{/opt/app/$target.*?:}{} } }

    Your hash is updated after that substitution if the regex matched, so I don't quite understand what your question is about.

      Oh I didn;t know its get updated after substitution!!! thanks any way Cheers