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


In reply to Re: Deletion of value in a multiple value assigned to a key by johngg
in thread Deletion of value in a multiple value assigned to a key by Raj84

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.