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

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. :)

Replies are listed 'Best First'.
Re: Stuck on a Perl assignment
by pryrt (Abbot) on Jan 25, 2017 at 23:16 UTC

    You are passing addtwo() an array reference (the square brackets []), but treating it like a hash reference (by using $params->{structure}). You might need to read up on perlref.

Re: Stuck on a Perl assignment
by stevieb (Canon) on Jan 26, 2017 at 00:37 UTC

    Along with having an array reference as opposed to a hash reference, ref($aref) returns ARRAY, not LIST, so that'll never catch.

    Here's a basic example for review and to do some testing with:

    :
    use warnings; use strict; my $struct = [ { 1 => 'a', 2 => 'b', }, [ 1, 2, 3, ], { 3 => 'c', 4 => 'd', }, ]; thing($struct); sub thing { my ($aref) = @_; for my $elem (@$aref){ if (ref $elem eq 'ARRAY'){ print "array:\n"; print "$_\n" for @$elem; } elsif (ref $elem eq 'HASH'){ print "hash:\n"; print "$_ => $elem->{$_}\n" for keys %$elem; } else { die "element is not an allowed ref\n"; } } }

    Output:

    hash: 1 => a 2 => b array: 1 2 3 hash: 3 => c 4 => d
Re: Stuck on a Perl assignment
by tybalt89 (Monsignor) on Jan 26, 2017 at 01:45 UTC
    #!/usr/bin/perl -l # http://perlmonks.org/?node_id=1180347 use strict; use warnings; use Data::Dumper; $Data::Dumper::Terse = 1; $Data::Dumper::Indent = 0; sub addone { my $arg = shift; ref $arg eq 'ARRAY' ? [ map addone($_), @$arg ] : ref $arg eq 'HASH' ? { map addone($_), %$arg } : $arg =~ /^\d+\z/ ? $arg + 1 : $arg; } print Dumper addone( [ { a => 1, b => 2, c => 3 }, { d => 4, e => 5 } +, [ 6, 7, 8 ], 9, 10, 11, [ 12, 13, 14 ] ] );
      I am still struggling with this code .. i cant figure out much after going through documentation ... this method works whats the error in this ..
      #!/usr/bin/perl use strict; use warnings 'all'; my $structure = [ { a => 1, b => 2, c => 3 }, { d => 4, e => 5 +}, [ 6, 7, 8 ], 9, 10, 11, [ 12, 13, 14 ] ]; sub addtwo { my $params = shift; my $s = $params->{$structure}; if (ref($s) eq "ARRAY") { my $c = 0; foreach my $e (@{$s}) { $s->[$c] = addtwo({ structur => $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 my $c; }
        A hash key is always a string. What do you the following does when $structure is an array reference?
        my $s = $params->{$structure};

        ($q=q:Sq=~/;[c](.)(.)/;chr(-||-|5+lengthSq)`"S|oS2"`map{chr |+ord }map{substrSq`S_+|`|}3E|-|`7**2-3:)=~y+S|`+$1,++print+eval$q,q,a,
        What is the error message?