2017-02-06 29 views
-1

我是新來的Perl和慢慢學習。我需要幫助來調試此代碼..看着以前的帖子我設法調試它,但我沒有得到任何輸出..我掙扎着,你們可以幫助我..非常感謝..任何提示或錯誤,你都注意到..有PERL增量匿名數組1的HASH參考問題通過1

#!/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 ] ]; 
print addtwo($structure); 
    sub addtwo { 

    my $params = shift; 
    my $s = $params->{$structure}; 

    if (ref($s) eq "ARRAY") { 
     my $c = 0; 
     foreach my $e (@{$s}) { 
      $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 my $c; 
} 

我已經提到這做了改變盡我所能,但沒有syntaxx錯誤或輸出,所以我卡住了。 Having HASH reference issue with perl

Not a HASH reference at main.pl line 13. 

從上面輸入

[ { a => 1, b => 2, c => 3 }, { d => 4, e => 5 }, [ 6, 7, 8 ], 9, 10, 11, [ 12, 13, 14 ] ] 

,我需要找回的輸出

[ { a => 2, b => 3, c => 4 }, { d => 5, e => 6 }, [ 7, 8, 9 ], 10, 11, 12, [ 13, 14, 15 ] ] 

任務是修改和添加語句來實現輸出。

另外,我寫了這個給我的理解

sub addone { 
    my ($aref) = @_; 
    for my $elem (@$aref){ 
     if (ref $elem eq 'ARRAY'){ 
      print "array:\n"; 
      my $var = "@$elem" ; 
      print "$_," for @$elem; 
     } 
     elsif (ref $elem eq 'HASH'){ 
      print "hash:\n"; 
      print "$_ => $elem->{$_}," for keys %$elem; 
     } 
     else { 
      $elem+1; 
      print "$elem,"; 
     } 
    } 
} 

並不如預期的輸出,因爲我無法訪問數組和散列值,以增加它。如何增加值並將其存儲在期望的輸出中?

我得到這個

Useless use of addition (+) in void context at main.pl line 29.                                  
a => 1,b => 2,c => 3,e => 5,d => 4,6,7,8,9,10,11,12,13,14, 
+5

你應該描述你的代碼應該做什麼,而不是讓人們從代碼中猜出你的意圖。標題應該更具描述性,少用大寫字母。 –

+0

以某種方式相關:http://stackoverflow.com/questions/41860863/having-hash-reference-issue-with-perl – mob

+0

這是一項家庭作業? – Borodin

回答

0

代碼的下面的修改似乎給您的預期輸出:

use strict; 
use warnings; 

use Data::Printer; 

my $structure = [ 
    { a => 1, b => 2, c => 3 }, 
    { d => 4, e => 5 }, 
    [ 6, 7, 8 ], 9, 10, 11, [ 12, 13, 14 ] 
]; 

sub addtwo { 
    my $s = shift; 

    if (ref($s) eq "ARRAY") { 
     for my $e (@$s) { 
      ref $e ? addtwo($e) : $e++; 
     } 
    } 
    elsif (ref($s) eq "HASH") { 
     for my $key (keys %$s) { 
      my $item = $s->{$key}; 
      ref $item ? addtwo($item) : $s->{$key}++; 
     } 
    } 
    else { 
     die "Unexpected input: ref \$s = " . (ref $s) . "\n"; 
    } 
} 

addtwo($structure); 
p $structure; 

輸出

[ 
    [0] { 
     a 2, 
     b 3, 
     c 4 
    }, 
    [1] { 
     d 5, 
     e 6 
    }, 
    [2] [ 
     [0] 7, 
     [1] 8, 
     [2] 9 
    ], 
    [3] 10, 
    [4] 11, 
    [5] 12, 
    [6] [ 
     [0] 13, 
     [1] 14, 
     [2] 15 
    ] 
] 
0

你有不同的問題,但它從這裏開始:你叫addtwo與裁判陣列($structure = [ … ]),這個函數內部它是由$params變量holded者和取消引用它作爲ref hash:$params->{...}$params不是ref散列,因此你的錯誤。

之後,我看不到你想要達到什麼樣的效果,以及你希望從輸入到輸出的轉換。

有(你的系統上man perlreftut)看看perlreftut上的Perl引用底漆

0

您的addone是在正確的方向。你只需要添加遞歸和值的修改。

sub add { 
    my $ref = ref($_[0]); 
    if ($ref eq ""  ) { ++$_[0];       } 
    elsif ($ref eq "ARRAY") { add($_) for @{ $_[0] };   } 
    elsif ($ref eq "HASH" ) { add($_) for values %{ $_[0] }; } 
    else      { croak("Unexpected input: $ref"); } 
}