Convert hash arrays

-1

I'm giving some maintenance on a perl system (language that I'm 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",
...
);

Can anyone help me with a way to do this transformation dynamically taking into consideration that the data contained in $data I am looking for in the DB.

I'm trying all day and I have not been able to do it until now, I have tried map and while but I do not think I'm doing it right.

    
asked by anonymous 28.07.2018 / 00:18

1 answer

1

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

Another point is that the way you manipulate the data coming from the bank may be 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 make the to / from your data is the form shown above where a counter is created to access the positions of the header and adding in the hash.

    
02.08.2018 / 21:40