如何在 Perl 中从 CSV 文件创建 HTML 下拉菜单?
我正在创建一个网页来从 CSV 文件中选择一些选项:
CSV 文件示例:
Time,h1,h2,h3,....
00:00:00,n1,n2,n3.....
.....so on
h -> header
n -> numbers
下面是我编写的用于过滤标题和值并返回的 Perl 代码子例程:
sub TimeData
{
use Text::CSV;
my @time;
my @data;
my ($csv_file, $type) = @_;
open(my $csv_fh, '<', $csv_file) or die $!;
my $parser = Text::CSV->new();
$parser->column_names( $parser->getline($csv_fh) );
while ( defined( my $hr = $parser->getline_hr($csv_fh) ) )
{
push @time, $hr->{Time};
push @data, $hr->{$type};
}
return (@time, @data);
}
我想创建一个页面,其中输入字段具有包含 h1、h2、h3 等列表的下拉菜单。所选标头值可以用作另一个 Perl 脚本中的输入。任何人都可以建议一些代码来完成此任务。
I'm creating a web page to select some options from a CSV file:
CSV File Sample:
Time,h1,h2,h3,....
00:00:00,n1,n2,n3.....
.....so on
h -> header
n -> numbers
Below is the Perl code subroutine I have written to filter the header and values and return:
sub TimeData
{
use Text::CSV;
my @time;
my @data;
my ($csv_file, $type) = @_;
open(my $csv_fh, '<', $csv_file) or die $!;
my $parser = Text::CSV->new();
$parser->column_names( $parser->getline($csv_fh) );
while ( defined( my $hr = $parser->getline_hr($csv_fh) ) )
{
push @time, $hr->{Time};
push @data, $hr->{$type};
}
return (@time, @data);
}
I want to create a page where the input field has a drop down menu with the lists of h1, h2, h3, etc. The selected header value can be used as a input in another Perl script. Can anyone please suggest some code to get this done.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
请注意,您的
return
语句是有问题的:两个数组将被展平为一个,并且调用代码将无法将返回值分配给两个单独的数组。如果您想保留该界面的精神,您应该返回对这些数组的引用。请参阅perldoc perlsub
:使用
split
因为我输入此内容的计算机既没有Text::CSV
也没有Text::xSV
]。现在,如果我正在做这样的事情,我会将读取数据的部分和生成
的部分分开,并为后者使用基于模板的方法。这是一个示例:
输出:
Note that your
return
statement is problematic: The two arrays will be flattened into one and the calling code will not be able to assign the return values to two separate arrays. If you want to keep the spirit of that interface, you should return references to those arrays. Seeperldoc perlsub
:Using
split
because the computer I am typing this on has neitherText::CSV
norText::xSV
].Now, if I were doing something like this, I would separate the part where I read the data and where I generate the
<SELECT></SELECT>
and use a template based approach for the latter. Here is an example:Output: