When you shift off a variable you pop it out of the argument . Just try and get the first variable of the argument another way by copying to an array first or something .
from perldoc Shift : Shifts the first value of the array off and returns it, shortening the array by 1 and moving everything down. If there are no elements in the array, returns the undefined value. If ARRAY is omitted, shifts the @_ array within the lexical scope of subroutines and formats, On Tue, Aug 23, 2011 at 10:23 AM, <anand.jaw...@emc.com> wrote: > Hi All, > > I am working on the below code to traverse through a hash, but it throws an > error which states "Can't coerce array into hash at temp.pl line 6." > > Code: > =============================================================== > sub hash_walk { > my $self = shift; > > my ($hash, $key_list, $callback) = @_; > > while (my ($k, $v) = each (%$hash)) { > push @$key_list, $k; > > if (ref($v) eq 'HASH') { > $self->hash_walk($v, $key_list, $callback); > > } > else { > $callback->($k, \$v, $key_list); > > } pop @$key_list; > > $hash->{$k} = $v; > } > } > my %data = ( > a => { > ab => 1, > ac => 2, > ad => { > ada => 3, > adb => 4, > adc => { > adca => 5, > adcb => 6, > }, > }, > }, > b => 7, > c => { > ca => 8, > cb => { > cba => 9, > cbb => 10, > }, > }, > ); > hash_walk(\%data, [], \&replace_all_val_strings); > sub replace_all_val_strings { > my ($k, $v, $key_list) = @_; > printf "k = %-8s v = %-4s key_list = [%s]\n", $k, $$v, "@$key_list"; > $$v =~ s/oldstr/newstr/; > printf "k = %-8s v = %-4s key_list = [%s]\n", $k, $$v, "@$key_list"; > } > =============================================================== > Could anyone please help me out. > > Thanks in Advance > Anand > -- Akinleye Adedamola