0

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

4 Answers 4

1

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. :)

Sign up to request clarification or add additional context in comments.

2 Comments

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

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

Comments

0

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

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.