in reply to Deletion of value in a multiple value assigned to a key
Using delete works for me. The code you show appears to be part of a Data::Dumper output without the variable name. Could it be that LOCKS is a key and not the name of your hash? This code demonstrates creating the hash, writing it to a file, recreating the hash from the file, deleting one sub-sub-key from the hash, writing the change back to the file again then recreating the hash again (I hold the file in a scalar rather than on disk just to keep things tidy).
use strict; use warnings; use 5.010; use Data::Dumper; my $hashFile; my $hashOutFH; my %HoH = ( LOCKS => { SCRIPT => { script1 => { params => [ qw{ a b c } ], shell => q{/bin/bash} }, script2 => { params => [ qw{ -v } ] }, }, }, ); open $hashOutFH, q{>}, \ $hashFile or die qq{open: > \ $hashFile: $!\n}; print $hashOutFH Data::Dumper->Dumpxs( [ \ %HoH ], [ qw{ *HoH } ] ); close $hashOutFH or die qq{close: > \ $hashFile: $!\n}; say q{File content after first write}; print $hashFile; say q{-} x 50; my %HoH1 = do { open my $hashInFH, q{<}, \ $hashFile or die qq{open: < \ $hashFile: $!\n}; my $str = <$hashInFH>; eval $str; %HoH; }; say q{New hash as read in from file}; print Data::Dumper->Dumpxs( [ \ %HoH1 ], [ qw{ *HoH1 } ] ); say q{-} x 50; delete $HoH1{ LOCKS }->{ SCRIPT }->{ script2 }; open $hashOutFH, q{>}, \ $hashFile or die qq{open: > \ $hashFile: $!\n}; print $hashOutFH Data::Dumper->Dumpxs( [ \ %HoH1 ], [ qw{ *HoH } ] ); close $hashOutFH or die qq{close: > \ $hashFile: $!\n}; say q{File content after key delete and second write}; print $hashFile; say q{-} x 50; my %HoH2 = do { open my $hashInFH, q{<}, \ $hashFile or die qq{open: < \ $hashFile: $!\n}; my $str = <$hashInFH>; eval $str; %HoH; }; say q{Second new hash read in from modified file}; print Data::Dumper->Dumpxs( [ \ %HoH2 ], [ qw{ *HoH2 } ] );
The output.
File content after first write %HoH = ( 'LOCKS' => { 'SCRIPT' => { 'script1' => { 'params' => [ 'a', 'b', 'c' ], 'shell' => '/bin/ba +sh' }, 'script2' => { 'params' => [ '-v' ] } } } ); -------------------------------------------------- New hash as read in from file %HoH1 = ( 'LOCKS' => { 'SCRIPT' => { 'script1' => { 'params' => [ 'a', 'b', 'c' ], 'shell' => '/bin/b +ash' }, 'script2' => { 'params' => [ '-v' ] } } } ); -------------------------------------------------- File content after key delete and second write %HoH = ( 'LOCKS' => { 'SCRIPT' => { 'script1' => { 'params' => [ 'a', 'b', 'c' ], 'shell' => '/bin/ba +sh' } } } ); -------------------------------------------------- Second new hash read in from modified file %HoH2 = ( 'LOCKS' => { 'SCRIPT' => { 'script1' => { 'params' => [ 'a', 'b', 'c' ], 'shell' => '/bin/b +ash' } } } );
I hope this is helpful.
Cheers,
JohnGG
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Deletion of value in a multiple value assigned to a key
by Raj84 (Initiate) on Mar 21, 2011 at 00:55 UTC | |
by Raj84 (Initiate) on Mar 21, 2011 at 06:45 UTC |