2012-01-18 52 views
0

示例使用PHP,但我想在perl中创建相同的关联二维数组,以及如何打印该数组。 EX在PHP中: -如何在perl中创建2维关联数组

while($row=mysql_fetch_array($rc)) 
{ 
    $associative_array[$ClientCode]=array('PartnerCode'=>$row["PartnerCode"], 
           'OldPartnerCode'=>$row["OldPartnerCode"], 
           'ClientCode'=>$ClientCode, 
           'OldClientCode'=> $row["OldClientCode"]); 
} 

添加OP的评论:

如果二维数组的创建是正确的,那么如何打印数组。

$sql_sel="select client_id,partner_id,client_code,partner_code from $tblname"; 
$sth = $dbh->prepare($sql_sel); 
$sth->execute(); 
$nrow=$sth->rows; 
while(($ccode,$pcode,$old_ccode,$old_pcode) = $sth->fetchrow_array) { 
    $ccode =~ y/A-Z/a-z/; 
    $pcode =~ y/A-Z/a-z/; 
    $old_ccode =~ y/A-Z/a-z/; 
    $old_pcode =~ y/A-Z/a-z/; 
    $client_partner_hash->{$ccode}={ 
     'newclinetcode'=>$ccode, 
     'newpartnercode'=>$pcode, 
     'oldclinetcode'=>$old_ccode, 
     'oldpartnercode'=>$old_pcode 
    }; 
} 

回答

1

下面是您发布的PHP代码的直接翻译。这并不完全相同。 :)

while (my $row = mysql_fetch_array($rc)) { 
    $associative_array->{$ClientCode} = { 
     'PartnerCode' => $row->{'PartnerCode'}, 
     'OldPartnerCode' => $row->{'OldPartnerCode'}, 
     'ClientCode'  => $ClientCode, 
     'OldClientCode' => $row->{'OldClientCode'}, 
    }; 
} 

注意,当然,Perl中没有mysql_fetch_array。相应翻译。 :)

+0

如果二维数组的创建是正确的,那么如何打印数组。 $ sql_sel =“从$ tblname中选择client_id,partner_id,client_code,partner_code”; $ sth = $ dbh-> prepare($ sql_sel); $ sth-> execute(); $ nrow = $ sth-> rows; (($ ccode,$ pcode,$ old_ccode,$ old_pcode)= $ sth-> fetchrow_array){ccode =〜y/A-Z/a-z/ $ pcode =〜y/A-Z/a-z /; $ old_ccode =〜y/A-Z/a-z /; $ old_pcode =〜y/A-Z/a-z /; $ client_partner_hash - > {$ CCODE} = { 'newclinetcode'=> $ CCODE, 'newpartnercode'=> $ P码, 'oldclinetcode'=> $ old_ccode, 'oldpartnercode'=> $ old_pcode}; } – 2012-01-18 07:55:13

+2

[的perldoc perllol](http://p3rl.org/perllol),[的perldoc perldsc](http://p3rl.org/perldsc),[的perldoc perlref](http://p3rl.org/perlref) |首先搜索存档文件很有礼貌:http://stackoverflow.com/search?q=print+2d+array+perl – daxim 2012-01-18 08:51:56

0

您可以获取二维数组中的行数据,并给出了如何显示它。

use Data::Dumper 
my $associate_array; #Array reference of the @D array 
while (my $row = $sth->fetchrow_arrayref()){ 
    $associative_array->[$ClientCode] = { 
     'PartnerCode' => $row->['PartnerCode'], 
     'OldPartnerCode' => $row->['OldPartnerCode'], 
     'ClientCode'  => $ClientCode, 
     'OldClientCode' => $row->['OldClientCode'], 
    }; 

} 

foreach my $row (@$associative_array){ 
    print Dumper $row; 
} 
0

您可以使用Data :: Dumper,因为有些人建议将哈希对象转储到屏幕上。你可能想要做更有用的事情。

foreach $ccode(keys(%$client_partner_hash)){ 
    print "C Code: $ccode\n"; 
    print "New Clinet Code: " . $client_partner_hash->{$ccode}->{newclinetcode} ."\n"; 
    print "New Partner Code: " .$client_partner_hash->{$ccode}->{newpartnercode} ."\n"; 
    print "Old Clinet Code: " . $client_partner_hash->{$ccode}->{oldclinetcode} ."\n"; 
    print "Old Partner Code: " .$client_partner_hash->{$ccode}->{oldpartnercode} ."\n"; 
} 
0

如果我们重新想象第一切割Perl版本融入了更多Perl的十岁上下的方式,我们可以得到这样的:

use strict; 
use warnings; 

my $sth = $dbh->prepare(
    "select client_id, partner_id, client_code, partner_code from $tblname" 
); 
$sth->execute(); 
my %client_partner_hash; 
while (my $row = $sth->fetchrow_hashref()) { 
    $client_partner_hash{lc($$row{client_code})} = +{ 
     map { $_ => lc($$row{$_}) } keys %$row 
    }; 
} 

如果你想出去再拉的数据,你可以这样做,说:

for my $ccode (sort keys %client_partner_hash) { 
    my $cp = $client_partner_hash{$ccode}; 

    print 'client_code = ', $$cp{client_code}, "\n"; 

    for my $part (sort keys %$cp} { 
     if ($part ne 'client_code') { 
      print " $part = ", $$cp{$part}, "\n"; 
     } 
    } 
}