Trouble converting array to hash


I have an array where elements of the array have values that are separated by tabs. For example:

client_name \t owner \t date \t port_number.

I need to convert that into a hash so it can be dumped into a MySQL database. Something like:

my %foo = ();
$foo{date} = "111208";
$foo{port} = "2222";
$foo{owner} = "ownername";
$foo{name} = "clientname";   

The problem I have is that there are duplicate client names but they exist on different port numbers. If I convert it directly to a hash using client_name as a key it will delete duplicate client names. The MySQL table is indexed based on {name} and {port}.

Is there any way I can convert this into a hash without losing duplicate client names?

my @record_list;
while ( <$generic_input> ) { 
     my $foo = {};
     @$foo{ qw<date port owner name> } = split /\t/;
     push @record_list, \%foo;

As a "pipeline" you could do this:

use List::MoreUtils qw<pairwise>;
my @fields = qw<date port owner name>;
my @records 
    = map {; { pairwise { $a => $b } @fields, @{[ split /\t/ ]}}}
By : Axeman

Why not just store it in a list (array)?

my @records = ();
while (my $line = <INFILE>) {
  chomp $line;
  my @fields = split /\t/ $line;
  push @records => { date => $fields[2],
                     name => $fields[0],
                     port => $fields[3],
                     owner => $fields[1] };
for my $record (@records) {
   $insert_query->execute (%$record);

This video can help you solving your question :)
By: admin