pankaj ghadge
pankaj ghadge

Reputation: 945

How to create 2 dimensional associative array in perl

Example is in PHP but i want to create same associative 2d array in perl and how to print that array. EX in PHP:-

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

Adding OP's comment:

If 2d array creation is correct then how to print that array.

$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
    }; 
}

Upvotes: 0

Views: 1425

Answers (4)

zgpmax
zgpmax

Reputation: 2847

If we re-imagine the first-cut Perl version into a more Perl-ish way, we might get something like this:

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
    };
}

If you want to pull the data out again, you could do, say:

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";
        }
    }
}

Upvotes: 0

Ilion
Ilion

Reputation: 6872

You can use Data::Dumper as some have suggested to dumper the hash object to the screen. You probably want to do something more useful.

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";
}

Upvotes: 0

ronak
ronak

Reputation: 79

You can get the row data in 2D array as below and also has given how to display it.

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;
}

Upvotes: 0

user149341
user149341

Reputation:

Here's a direct translation of the PHP code you posted. It's not all that different. :)

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

Note, of course, that there's no mysql_fetch_array in Perl. Translate accordingly. :)

Upvotes: 1

Related Questions