in reply to Stuck on a Perl assignment

#!/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 ] ] );

Replies are listed 'Best First'.
Re^2: Stuck on a Perl assignment
by Anonymous Monk on Feb 06, 2017 at 20:32 UTC
    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?