Hello guys! I just started to study Perl and I am kinda stuck on a piece of code. I need to write a program that takes an input list of hashes and lists - [ { a => 1, b => 2, c => 3 }, { d => 4, e => 5 }, [ 6, 7, 8 ], 9, 10, 11, [ 12, 13, 14 ] ] and it increments all the integer values by 1. Please take a look below.
sub addtwo { my $params = shift; my $s = $params->{structure}; # if (ref($s) eq "LIST") { $c = 0; foreach $e (@{$s}) { push @$s, addtwo({ structures => $e }); #$s->[$c] = addtwo({ structure => $e }); $c++; } # } elsif (ref($s) eq "HASH") { if (scalar keys %{$s} == 0) { return undef; } else { foreach my $e (values %{$s}) { $s->{$e} = addtwo({ structure => $s->{$e} }); } } } else { $s = 1; } return $c; } print addtwo([ { a => 1, b => 2, c => 3 }, { d => 4, e => 5 }, [ 6, 7 +, 8 ], 9, 10, 11, [ 12, 13, 14 ] ]);
I am getting the following error: "Not a HASH reference at D:/workspace/test/test.pl line 5." I would really appreciate if you could help me out on this one. :) Thank you for your time guys. :)
In reply to Stuck on a Perl assignment by ickopicko
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |