将数组转换为哈希时遇到问题

发布于 2024-12-20 09:25:51 字数 404 浏览 2 评论 0原文

我有一个数组,其中数组元素的值由制表符分隔。 例如:

客户端名称\t 所有者\t 日期\t 端口号。

我需要将其转换为哈希值,以便将其转储到 MySQL 数据库中。 类似于:

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

我遇到的问题是存在重复的客户端名称,但它们存在于不同的端口号上。如果我使用 client_name 作为键将其直接转换为哈希,它将删除重复的客户端名称。 MySQL 表基于 {name} 和 {port} 建立索引。

有什么方法可以将其转换为哈希而不丢失重复的客户端名称?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

帥小哥 2024-12-27 09:25:51

您将遍历您的文件,像您所做的那样构建哈希,然后将该哈希的引用推送到数组上。类似于:

foreach my $line ( @lines ) {
  # Make your %foo hash.
  push @clients, \%foo;
}

然后,当您插入数据库时​​,您只需迭代 @clients 中的元素:

foreach my $client ( @clients ) {
  $date = $client->{'date'};
  ...
}

编辑: 如果您想将其转换为哈希值的哈希值,然后当您循环遍历行列表时,您将执行以下操作:

foreach my $line ( @lines ) {
  # Make your %foo hash.
  $clients{$foo{'port'}} = \%foo;
}

然后您将获得使用端口号作为键的哈希值的哈希值。

You would go through your file, build up the hash like you've done, then push a reference to that hash onto an array. Something like:

foreach my $line ( @lines ) {
  # Make your %foo hash.
  push @clients, \%foo;
}

Then afterwards, when you're inserting into your DB, you just iterate through the elements in @clients:

foreach my $client ( @clients ) {
  $date = $client->{'date'};
  ...
}

Edit: If you want to turn this into a hash of hashes, then as you loop through the list of lines, you'd do something like:

foreach my $line ( @lines ) {
  # Make your %foo hash.
  $clients{$foo{'port'}} = \%foo;
}

Then you'll have a hash of hashes using the port number as the key.

惯饮孤独 2024-12-27 09:25:51

为什么不将其存储在列表(数组)中?

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

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);
}
一桥轻雨一伞开 2024-12-27 09:25:51
my @record_list;
while ( <$generic_input> ) { 
     my $foo = {};
     @$foo{ qw<date port owner name> } = split /\t/;
     push @record_list, \%foo;
 }

作为“管道”,你可以这样做:

use List::MoreUtils qw<pairwise>;
my @fields = qw<date port owner name>;
my @records 
    = map {; { pairwise { $a => $b } @fields, @{[ split /\t/ ]}}}
      <$input>
    ;
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/ ]}}}
      <$input>
    ;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文