duan4369 2010-06-18 20:43
浏览 192
已采纳

循环遍历数组以在Perl中构建多维数组,从PHP转换,并在语法上错误

Working on converting an array to another array. In PHP, this is easy, but Perl has some syntax that I am having a hard time getting my head around and understanding.

Here is my loop in Perl:

foreach my $r (@toindex){
    #print Dumper $r;
    %indexed{@$r[0]}{'image_id'} = @$r[0];     #Broken
    %indexed{"@$r[0]"}{'image_id'} = @$r[0];   #Broken
}

Here is my @toindex array

$VAR1 = [
      [
        3638584,
        'Aperture',
        'F13'
      ],
      [
        3638588,
        'Exposure Bias',
        '0 EV'
      ],
      [
        3638588,
        'Focal Length',
        '80.0 mm'
      ],
    ];

And here is what I want to do, but in PHP

foreach($indexrows as $k => $v){
    $indexed[$v['image_id']]['image_id'] = $v['image_id'];     
}

It seems so very simple in PHP, but moving it to Perl is proving to be quite a challenge for me.


Update

Thanks to the help of Sinan Ünür and DVK with that final little pointer, I have a working solution. I'm posting the complete script in case anyone might find some part of it useful in the future.

#!/usr/bin/perl
use strict; use warnings; use DBI; use Data::Dumper;

my $dbh = DBI->connect('dbi:Pg:dbname=database;host=serveraddress','user','password') or die;
my $sth;
my $sql = "SELECT id, field, data FROM table";

my $offset = 0; 
my $increment = 20;
my $toindex;

# This loop here is to solve a problem that was not part of the
# original question. I included it to illustrate the syntax for
# looping a database query
do{
    $sth = $dbh->prepare($sql . " LIMIT " . $increment . " OFFSET " . $offset);
    $sth->execute or die;
    $toindex = $sth->fetchall_arrayref;
    $offset = $offset + $increment;
}while(@$toindex == 0);

# Alternately, if you do not need a loop below is all you need
# $sth = $dbh->prepare($sql);
# $sth->execute or die;
# $toindex = $sth->fetchall_arrayref;

my %indexed;
foreach my $r ( @$toindex ) {
    #print Dumper $r;
    my ($id, $field, $value) = @$r;
    @{ $indexed{ $id } }{('image_id', $field)} = ($id, $value);
}

print Dumper %indexed; 

$dbh->disconnect;
  • 写回答

3条回答 默认 最新

  • dongwei6457 2010-06-18 21:01
    关注

    I am going to speculate that you are trying to convert that information to a hash table indexed by the image identifier:

    #!/usr/bin/perl
    
    use strict; use warnings;
    
    my $table = [
          [ 3638584 => 'Aperture',      'F13'     ],
          [ 3638588 => 'Exposure Bias', '0 EV'    ],
          [ 3638588 => 'Focal Length',  '80.0 mm' ],
    ];
    
    my %indexed;
    
    for my $r ( @$table ) {
        @{ $indexed{ $r->[0] } }{('image_id', $r->[1])} = @$r[0,2];
    }
    
    use YAML;
    print Dump \%indexed;
    

    Output:

    E:\Home> t
    ---
    3638584:
      Aperture: F13
      image_id: 3638584
    3638588:
      Exposure Bias: 0 EV
      Focal Length: 80.0 mm
      image_id: 3638588

    You can write the for loop above less cryptically as:

    for my $r ( @$table ) {
        my ($id, $field, $value) = @$r;
        @{ $indexed{ $id } }{('image_id', $field)} = ($id, $value);
    }
    

    which might save a lot of headaches a week from now.

    See also the Perl Data Structures Cookbook. Perl comes with excellent documentation; use it.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 请完成下列相关问题!
  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?
  • ¥15 求daily translation(DT)偏差订正方法的代码
  • ¥15 js调用html页面需要隐藏某个按钮
  • ¥15 ads仿真结果在圆图上是怎么读数的
  • ¥20 Cotex M3的调试和程序执行方式是什么样的?
  • ¥20 java项目连接sqlserver时报ssl相关错误
  • ¥15 一道python难题3
  • ¥15 牛顿斯科特系数表表示
  • ¥15 arduino 步进电机