Convert arrays to hash

Asked

Viewed 59 times

-1

I am giving some maintenance on a system in Perl (language that I am Noob) and I needed to create a new report module where I need to solve the following situation:

I have 2 arrays with the following formats

@head = ("nomeinstituicao", "cnpjinstituicao","nomecliente", "cnpjcliente", "notacliente" );

@data = ("inst1", "12345678000112","joao", 
 "87654321000198","5","inst2","54387612000123","maria","45612387000123","6",...);

I need to produce a hash in the following format:

%hash = (

"nomeinstituicao" => "inst1",
"cnpjinstituicao" => "12345678000112",
"nomecliente" => "joao",
"cnpjcliente" => "87654321000198",
"notacliente" => "5",
"nomeinstituicao" => "inst2",
"cnpjinstituicao" => "54387612000123",
"nomecliente" => "maria",
"cnpjcliente" => "45612387000123",
"notacliente" => "6",
...
);

Could someone help me with a way to make this transformation dynamically taking into account that data contained in $data I’m looking in the comic book.

I’ve been trying all day and I haven’t been able to so far, I’ve tried with map and with while but I guess I’m not doing it right.

2 answers

0

What you need to do is to/from the data you have in the database with the header that you have.

Another point is that the way you manipulate the data coming from the database can be them in array or hashref for example.

By your example I believe it is the first case with the result set being in an array.

Thus:

my @head = ('nomeinstituicao', 'cnpjinstituicao','nomecliente', 'cnpjcliente', 'notacliente' );

my @data = ('inst1', '12345678000112','joao','87654321000198','5');


my $c = 0;

my %hash;

foreach my $d ( @data )
{

    $hash{$head[$c]} = $d;

    $c++;

}

use Data::Dumper;

print Dumper \%hash;

A simple way to do the from/to with your data is the form shown above where a counter is created for access the positions needed from header and adding to hash.

0

In fact you don’t really want a hash as an answer, you want a hash per line, an array of hashes, otherwise you will overwrite the elements of your hash. So here’s the modified code:

use Data::Dumper;

my @head = ("nomeinstituicao", "cnpjinstituicao","nomecliente", "cnpjcliente", "notacliente");

my @data = ("inst1", "12345678000112","joao",  "87654321000198","5",
         "inst2","54387612000123","maria","45612387000123","6");


my @hash;
my $realhash;
for my $i (0..$#data) {
    my $coluna = $head[$i % $#head];
    if ($coluna eq "nomeinstituicao") {
        push @hash, $realhash if ($realhash);
        $realhash = {};
    }
    $realhash->{$coluna} = $data[$i];
}

print Dumper \@hash;

This code produces the following output:

$VAR1 = [
          {
            'nomecliente' => 'joao',
            'cnpjcliente' => '87654321000198',
            'cnpjinstituicao' => '12345678000112',
            'nomeinstituicao' => 'inst1'
          },
          {
            'cnpjinstituicao' => 'inst2',
            'cnpjcliente' => 'maria',
            'nomecliente' => '54387612000123',
            'nomeinstituicao' => '5'
          }
        ];

If you are using DBI, there is a method that does it all for you, without you having to build the arrays @head @data, is called fetchall_hashref

Browser other questions tagged

You are not signed in. Login or sign up in order to post.