in reply to Accessing (deleting) array elements in a hash of arrays
I think this is what you're describing. A hash of arrays contains data which you may wish to selectively delete by specifying which array in the hash, and which element to delete. This code only allows one element at a time to be specified per array, but it wouldn't be hard to support multiple elements to be deleted if needed. Rather than use delete, I create a new anonymous array reference with [ EXPR ] and the array itself is made by grep EXPR ARRAY which converts the original array into a array of elements which do not equal the element to delete. Hope that helps. SSF#!/usr/bin/perl use strict; use Data::Dumper; my %hashOfArrays=( a=>[1,2,3], b=>[4,5,6], c=>[7,8,9] ); my %toDelete=( a=>2, b=>6, c=>8 ); for my $array (keys %hashOfArrays) { if ($toDelete{$array}) { $hashOfArrays{$array}=[ grep { $_ != $toDelete{$array} } @{$hashOfArrays{$array}} ]; } } print Dumper(\%hashOfArrays); # prints $VAR1 = { # 'c' => [ # 7, # 9 # ], # 'a' => [ # 1, # 3 # ], # 'b' => [ # 4, # 5 # ] # }; exit;
|
|---|