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

Dear All,

If I have a hash $foo, with keys $bar, and I store a variable length array with values at index $idx, such that

$foo{$bar}->[$idx]

is a value, then:

* how do I delete an element (say, at position $idx) from the array I have referenced from the hash?

'Delete' does not seem to do the trick, and I am not sure how to use 'Splice' in this specific example.

Thanks in advance! :)

Replies are listed 'Best First'.
Re: Deleting an element in an array referenced by a hash element
by Corion (Patriarch) on Jul 12, 2010 at 10:57 UTC

    See tye's References Quick Reference for how to dereference things:

    use strict; use Data::Dumper; my $idx = 1; my %foo = ( bar => [1, 2, 3, ], ); print "foo before: " . Dumper \%foo; my $bar = 'bar'; my @deleted = splice @{ $foo{ $bar }}, $idx, 1; print "foo after: " . Dumper \%foo; print "Deleted: " . Dumper \@deleted; __END__ foo before: $VAR1 = { 'bar' => [ 1, 2, 3 ] }; foo after: $VAR1 = { 'bar' => [ 1, 3 ] }; Deleted: $VAR1 = [ 2 ];

      @Corion:

      Perfect! Thanks for such a succinct and complete explanation ;-)

Re: Deleting an element in an array referenced by a hash element
by jethro (Monsignor) on Jul 12, 2010 at 10:56 UTC
    splice( @{$foo{$bar}},$idx,1 );

    delete only works with hashes

      delete also works with arrays but it just deletes the value, it doesn't change the size of the array.