只从perl中的列表获取某些值 [英] only taking certain values from a list in perl

查看:154
本文介绍了只从perl中的列表获取某些值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

首先,我将描述我的问题,然后问题。

First I will describe what I have, then the problem.

我有一个文本文件,结构如下

I have a text file that is structured as such

----------- Start of file-----
<!-->
name,name2,ignore,name4,jojobjim,name3,name6,name9,pop
-->
<csv counter="1">
1,2,3,1,6,8,2,8,2,
2,6,5,1,5,8,7,7,9,
1,4,3,1,2,8,9,3,4,
4,1,6,1,5,6,5,2,9
</csv>
-------- END OF FILE-----------

我也有一个perl程序有一个地图:

I also have a perl program that has a map:

 my %column_mapping = (
"name" => 'name',
"name1" => 'name_1',
"name2" => 'name_2',
"name3" => 'name_3',
"name4" => 'name_4',
"name5" => 'name_5',
"name6" => 'name_6',
"name7" => 'name_7',
"name9" => 'name_9',
)

我的动态插入语句(假设我连接到数据库,头是我的数组的头名称,如test1,test2,ect)

My dynamic insert statement (assume I connected to database proper, and headers is my array of header names, such as test1, test2, ect)

my $sql = sprintf 'INSERT INTO tablename ( %s ) VALUES ( %s )',
    join( ',', map { $column_mapping{$_} } @headers ),
    join( ',', ('?') x scalar @headers ); 

my $sth = $dbh->prepare($sql);

现在的问题我实际上有:
我需要一种方法,插入标头和地图中的值。
在作为示例给出的数据文件中,有几个不在映射中的名称,是否有一种方法可以忽略它们和在csv段中与它们相关联的数字?

Now for the problem I am actually having: I need a way to only do an insert on the headers and for the values that are in the map. In the data file given as an exmaple, there are several names that are not in the map, is there a way I can ignore them and the numbers associated with them in the csv section?

基本上将子集csv转换为:

basically to make a subset csv, to turn it into:

name,name2,name4,name3,name6,name9,
 1,2,1,8,2,8,
 2,6,1,8,7,7,
 1,4,1,8,9,3,
 4,1,1,6,5,2,

我的插入统计只会插入地图中的那些。

so that my insert statment will only insert the ones in the map. The data file is always different, and are not in same order, and an unknown amount will be in the map.

理想情况下,这是一个有效的方法,因为这个脚本将会经历成千上万的文件,每个文件后面百万行的csv与几百列。

Ideally a efficient way to do this, since this script will be going through thousands of files, and each files behind millions of lines of the csv with hundreds of columns.

这只是一个文本文件正在读取,而不是一个

It is just a text file being read though, not a csv, not sure if csv libraries can work in this scenario or not.

推荐答案

您通常将有效的索引集合放在列表,然后使用阵列切片

You would typically put the set of valid indices in a list and use array slices after that.

@valid = grep { defined($column_mapping{ $headers[$_] }) } 0 .. $#headers;

...

my $sql = sprintf 'INSERT INTO tablename ( %s ) VALUES ( %s )',
  join( ',', map { $column_mapping{$_} } @headers[@valid] ),
  join( ',', ('?') x scalar @valid);
my $sth = $dbh->prepare($sql);

...

my @row = split /,/, <INPUT>; 
$sth->execute( @row[@valid] );

...

这篇关于只从perl中的列表获取某些值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆