将数组转换为哈希时遇到问题
我有一个数组,其中数组元素的值由制表符分隔。 例如:
客户端名称\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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您将遍历您的文件,像您所做的那样构建哈希,然后将该哈希的引用推送到数组上。类似于:
然后,当您插入数据库时,您只需迭代
@clients
中的元素:编辑: 如果您想将其转换为哈希值的哈希值,然后当您循环遍历行列表时,您将执行以下操作:
然后您将获得使用端口号作为键的哈希值的哈希值。
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:
Then afterwards, when you're inserting into your DB, you just iterate through the elements in
@clients
: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:
Then you'll have a hash of hashes using the port number as the key.
为什么不将其存储在列表(数组)中?
Why not just store it in a list (array)?
作为“管道”,你可以这样做:
As a "pipeline" you could do this: