免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 2382 | 回复: 8
打印 上一主题 下一主题

难道perl类只能存两个数组的数组,附代码,求解答 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2007-12-21 17:09 |只看该作者 |倒序浏览
Bean.pm
package Bean;

use strict;
use Class::Struct;

struct Bean =>
{
  name => '$',
  list => '@',
};

1;


test.pl

use strict;
use Bean;

my $bean;
my @l;
my @list;
my @beans;

@l = ('111', '112', '113');
push(@list, [@l]);
@l = ('121', '122', '123');
push(@list, [@l]);
@l = ('131', '132', '133');
push(@list, [@l]);
@l = ('141', '142', '143');
push(@list, [@l]);
$bean = Bean->new();
$bean->name("name1");
$bean->list(@list);
push(@beans, $bean);

@l = ('211', '212', '213');
push(@list, [@l]);
@l = ('221', '222', '223');
push(@list, [@l]);
@l = ('231', '232', '233');
push(@list, [@l]);
@l = ('241', '242', '243');
push(@list, [@l]);
$bean = Bean->new();
$bean->name("name2");
$bean->list(@list);
push(@beans, $bean);

@l = ('311', '312', '313');
push(@list, [@l]);
@l = ('321', '322', '323');
push(@list, [@l]);
@l = ('331', '332', '333');
push(@list, [@l]);
@l = ('341', '342', '343');
push(@list, [@l]);
$bean = Bean->new();
$bean->name("name3");
$bean->list(@list);
push(@beans, $bean);

@l = ('411', '412', '413');
push(@list, [@l]);
@l = ('421', '422', '423');
push(@list, [@l]);
@l = ('431', '432', '433');
push(@list, [@l]);
@l = ('441', '442', '443');
push(@list, [@l]);
$bean = Bean->new();
$bean->name("name4");
$bean->list(@list);
push(@beans, $bean);


foreach(@beans)
{
  my $name = $_->name();
  my $list = $_->list();

  print 'name is:' . $name . "\n";
  foreach(@$list)
  {
    my $list = $_;
    foreach(@$list)
    {
      my $str = $_;
      print $str . "\n";
    }
  }
}


报错信息是
Too many args to list at E:\online\oop\test3\test.pl line 20

论坛徽章:
0
2 [报告]
发表于 2007-12-21 17:13 |只看该作者
如果控制scalar(@list)为两个就没问题,为什么呢?
我原来是写java的,对于perl这样的数据结构现在很迷惑.....

论坛徽章:
1
2015年辞旧岁徽章
日期:2015-03-03 16:54:15
3 [报告]
发表于 2007-12-21 17:17 |只看该作者
太复杂了,搞个简单的,能说明问题的上来。

论坛徽章:
0
4 [报告]
发表于 2007-12-21 17:26 |只看该作者

use strict;
use Bean;

my $bean;
my @l;
my @list;

@l = ('111', '112', '113');
push(@list, [@l]);
@l = ('121', '122', '123');
push(@list, [@l]);
@l = ('131', '132', '133');
push(@list, [@l]);
@l = ('141', '142', '143');
push(@list, [@l]);

$bean = Bean->new();
$bean->name("name1");
$bean->list(@list);


my $listtmp = $bean->list();
foreach(@$listtmp)
{
  my $listtmp2 = $_;
  foreach(@$listtmp2)
  {
    my $str = $_;
    print $str . "\n";
  }
}

论坛徽章:
0
5 [报告]
发表于 2007-12-21 17:26 |只看该作者
您给分析分析

论坛徽章:
0
6 [报告]
发表于 2007-12-21 22:57 |只看该作者
Class::Struct里的文档说的很清楚:

Array ('@' or '*@')

    The element is an array, initialized by default to ().

    With no argument, the accessor returns a reference to the element's whole array (whether or not the element was specified as '@' or '*@').

    With one or two arguments, the first argument is an index specifying one element of the array; the second argument, if present, is assigned to the array element. If the element type is '@', the accessor returns the array element value. If the element type is '*@', a reference to the array element is returned.

    As a special case, when the accessor is called with an array reference as the sole argument, this causes an assignment of the whole array element. The object reference is returned.

论坛徽章:
0
7 [报告]
发表于 2007-12-21 23:09 |只看该作者
$bean->list(@list);
是错误的,struct里面是hash,其value只能为标量,所以上面代码应该是:
$bean->list(\@list);

论坛徽章:
0
8 [报告]
发表于 2007-12-21 23:30 |只看该作者
Hi,
just like Nosferatu said, pls read more perldoc in details.
this is demo code  4u:

  1. use Class::Struct;

  2. struct Breed => {
  3.     name  => '$',
  4.     cross => '$',
  5. };

  6. struct Cat => [
  7.     name     => '$',
  8.     kittens  => '@',
  9.     markings => '%',
  10.     breed    => 'Breed',
  11.    
  12.    
  13. ];

  14. struct Bean => {
  15.     list => '@',
  16. };

  17. # main

  18. my $cat = Cat->new(
  19.     name     => 'Socks',
  20.     kittens  => ['Monica', 'Kenneth'],
  21.     markings => { socks=>1, blaze=>"white" },
  22.     breed    => Breed->new(name=>'short-hair', cross=>1),
  23. );   

  24. # Add more name in the list of kittens
  25. push @{$cat->kittens},  ("miao", "mimi", "mini");

  26. print "Once a cat called ", $cat->name, "\n";
  27. print "(which was a ", $cat->breed->name, ")\n";
  28. print "had " . scalar(@{$cat->kittens}) . " kittens: ", join(', ', @{$cat->kittens}), "\n";

  29. #### your Bean for example:
  30. my $obj = Bean->new();
  31. my @arr1 = (1,2,3);
  32. my @arr2 = (4,5,6);
  33. push @{$obj->list}, @arr1; # don't use reference of array !!!
  34. push @{$obj->list}, @arr2;
  35. print "\$obj->list: ", join ", ", @{$obj->list}, "\n";

复制代码


Mery Christmas and Happy New year!

-- ulmer

论坛徽章:
0
9 [报告]
发表于 2007-12-23 15:23 |只看该作者
谢谢各位,找到错误了

$bean->list(@list);
应该是
$bean->list([@list]);
就好了,^.^
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP